{"id":1569,"date":"2009-08-06T10:43:58","date_gmt":"2009-08-06T09:43:58","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=1569"},"modified":"2009-08-06T10:43:58","modified_gmt":"2009-08-06T09:43:58","slug":"mathematicas-table-function-not-always-the-fastest-way-to-make-a-table","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=1569","title":{"rendered":"Mathematica&#8217;s Table function &#8211; not always the fastest way to make a table"},"content":{"rendered":"<p>One of the things I love most about blogging is the interaction with readers via the comments section.\u00a0 Put simply you guys know your onions and although I (hopefully) have something useful and\/or interesting to tell you, there is a heck of a lot of stuff that you could tell me.<\/p>\n<p>In the comments section of one of my recent posts, Sander Huisman, did exactly that when he suggested that I shouldn&#8217;t always use Table in <a href=\"http:\/\/www.wolfram.com\/\">Mathematica<\/a> when generating lists.\u00a0 This was news to me &#8211; I thought that Table was the accepted and possibly the fastest way to do it in Mathematica but it seems not.\u00a0 Here is a concrete example as provided by Sander.<\/p>\n<pre>AbsoluteTiming[Table[PrimeQ[i], {i, 0, 10000000}];]\r\nAbsoluteTiming[PrimeQ \/@ Range[0, 10000000];]<\/pre>\n<p>Both of these Mathematica expressions test each of the first 10000000 integers (including 0) to see if they are prime and what you end up with is a list of Booleans &#8211; {False, False, True, True, False, True, False, True, False, False} and so on.  The difference is that the first expression is noticeably slower than the second.<\/p>\n<p>If, like me, you struggle to remember what the punctuation operators (\/. \/@ etc) actually do in Mathematica then  know that <strong>foo \/@ bar<\/strong> is equivalent to <strong>Map[foo,bar]<\/strong> where <strong>foo <\/strong>is a funbction and <strong>bar<\/strong> is a list.<\/p>\n<p>On one of my test machines, the first expression evaluated in 10.012 seconds on average whereas the second evaluated in 8.28 seconds on average (both averages are over 100 runs).<\/p>\n<p>So Map is faster than Table right?<\/p>\n<p>Maybe, maybe not.\u00a0 Try the following examples (again provided by Sander) where we generate a list of i+2 for i between 1 and 10^7.<\/p>\n<pre>AbsoluteTiming[ Table[i + 2, {i, 10^7}];]\r\nAbsoluteTiming[# + 2 &amp; \/@ Range[10^7];]<\/pre>\n<p>For this calculation Table did it in 0.66 seconds and \/@ (or Map) did it in 0.96 seconds (both averaged over 100 runs).  As Sander says<strong> &#8216;Sometimes it is worth looking in to the way you make a list!&#8217;.<\/strong><em><\/em><\/p>\n<p>Comments welcomed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things I love most about blogging is the interaction with readers via the comments section.\u00a0 Put simply you guys know your onions and although I (hopefully) have something useful and\/or interesting to tell you, there is a heck of a lot of stuff that you could tell me. In the comments section [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8,7],"tags":[],"class_list":["post-1569","post","type-post","status-publish","format-standard","hentry","category-math-software","category-mathematica","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-pj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1569"}],"version-history":[{"count":4,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":1575,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions\/1575"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}