{"id":25063,"date":"2017-06-08T21:36:04","date_gmt":"2017-06-08T16:06:04","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25063"},"modified":"2017-06-08T21:36:04","modified_gmt":"2017-06-08T16:06:04","slug":"performance-of-loops","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/performance-of-loops\/","title":{"rendered":"Performance of loops"},"content":{"rendered":"<p>Consider below two <a href=\"https:\/\/www.wikitechy.com\/technology\/analysis-of-loops\/\">C language functions<\/a> to compute sum of elements in a 2D array. Ignoring the compiler optimizations, which of the two is better implementation of sum?<\/p>\n[ad type=&#8221;banner&#8221;]\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Function 1<br\/>int fun1(int arr[R][C])<br\/>{<br\/>    int sum = 0;<br\/>    for (int i=0; i&lt;R; i++)<br\/>      for (int j=0; j&lt;C; j++)<br\/>          sum += arr[i][j];<br\/>}<br\/> <br\/>\/\/ Function 2<br\/>int fun2(int arr[R][C])<br\/>{<br\/>    int sum = 0;<br\/>    for (int j=0; j&lt;C; j++)<br\/>      for (int i=0; i&lt;R; i++)<br\/>          sum += arr[i][j];<br\/>}<\/code><\/pre> <\/div>\n<p>In C\/C++, elements are stored in Row-Major order. So the first implementation has better spatial locality (nearby memory locations are referenced in successive iterations). Therefore, first implementation should always be preferred for iterating multidimensional arrays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Performance of loops &#8211; Analysis of Algorithm &#8211; Consider below two C language functions to compute sum of elements in a 2D array.<\/p>\n","protected":false},"author":1,"featured_media":28413,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70146],"tags":[71410,71406,82916,82907,82918,82908,82923,82919,82915,78797,82922,82917,82914,78784,82912,82921,71407,71404,82925,82924,5660,82913,82903,5671,82911,82920,82904,82905,82926,82909,71408,82910,71405,71409,82906,71411],"class_list":["post-25063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analysis-of-algorithm","tag-algorithm-efficiency-in-data-structures","tag-algorithm-performance-analysis-pdf","tag-closed-loop-feedback","tag-control-loop","tag-control-loop-monitoring","tag-control-loop-performance-monitoring","tag-enhanced-for-loop-in-java","tag-enhanced-for-loop-java","tag-event-loop","tag-for-each-loop","tag-for-in-loop-javascript","tag-for-loop-in-java","tag-for-loop-in-javascript","tag-for-loop-java","tag-for-loop-javascript","tag-foreach-loop","tag-how-to-calculate-the-efficiency-of-an-algorithm","tag-how-to-measure-algorithm-efficiency","tag-java-enhanced-for-loop","tag-javascript-array-loop","tag-javascript-for-loop","tag-javascript-for-loop-array","tag-javascript-for-of-performance","tag-javascript-foreach-loop","tag-javascript-loop","tag-javascript-loop-array","tag-javascript-loop-performance","tag-javascript-reverse-for-loop","tag-loop-llc","tag-loop-performance","tag-performance-analysis-of-an-algorithm-in-daa","tag-performance-loop","tag-performance-measurement-in-data-structure","tag-performance-measurement-of-an-algorithm","tag-performance-of-loops","tag-what-is-usually-the-main-factor-that-determines-the-speed-of-an-algorithm"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25063"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25063\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/28413"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}