{"id":5377,"date":"2014-02-26T20:09:07","date_gmt":"2014-02-26T19:09:07","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5377"},"modified":"2014-02-27T00:39:28","modified_gmt":"2014-02-26T23:39:28","slug":"matlab-repeated-multiplication-is-faster-then-integer-powers","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5377","title":{"rendered":"MATLAB: Repeated multiplication is faster than integer powers"},"content":{"rendered":"<p>Consider the following code<\/p>\n<pre>function testpow() \r\n%function to compare integer powers with repeated multiplication\r\n\r\nrng default\r\na=rand(1,10000000)*10;\r\n\r\ndisp('speed of ^4 using pow')\r\ntic;test4p = a.^4;toc\r\ndisp('speed of ^4 using multiply')\r\ntic;test4m = a.*a.*a.*a;toc\r\n\r\ndisp('maximum difference in results');\r\nmax_diff = max(abs(test4p-test4m))\r\n\r\nend<\/pre>\n<p>On running it on my late 2013 Macbook Air with a Haswell Intel i5 using MATLAB 2013b, I get the following results<\/p>\n<pre>speed of ^4 using pow\r\nElapsed time is 0.527485 seconds.\r\nspeed of ^4 using multiply\r\nElapsed time is 0.025474 seconds.\r\nmaximum difference in results\r\nmax_diff =\r\n   1.8190e-12<\/pre>\n<p>In this case (derived from a real-world case), using repeated multiplication is around twenty times faster than using integer powers in MATLAB. This leads to some questions:-<\/p>\n<ul>\n<li>Why the huge speed difference?<\/li>\n<li>Would a similar speed difference be seen in other systems&#8211;R, Python, Julia etc?<\/li>\n<li>Would we see the same speed difference on other operating systems\/CPUs?<\/li>\n<li>Are there any numerical reasons why using repeated multiplication instead of integer powers is a bad idea?<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following code function testpow() %function to compare integer powers with repeated multiplication rng default a=rand(1,10000000)*10; disp(&#8216;speed of ^4 using pow&#8217;) tic;test4p = a.^4;toc disp(&#8216;speed of ^4 using multiply&#8217;) tic;test4m = a.*a.*a.*a;toc disp(&#8216;maximum difference in results&#8217;); max_diff = max(abs(test4p-test4m)) end On running it on my late 2013 Macbook Air with a Haswell Intel i5 [&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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53,11,7],"tags":[],"class_list":["post-5377","post","type-post","status-publish","format-standard","hentry","category-making-matlab-faster","category-matlab","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1oJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5377","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=5377"}],"version-history":[{"count":6,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5377\/revisions"}],"predecessor-version":[{"id":5386,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5377\/revisions\/5386"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}