{"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=\u201dbanner\u201d]\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20Function%201%0Aint%20fun1(int%20arr%5BR%5D%5BC%5D)%0A%7B%0A%20%20%20%20int%20sum%20%3D%200%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3CR%3B%20i%2B%2B)%0A%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3CC%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20sum%20%2B%3D%20arr%5Bi%5D%5Bj%5D%3B%0A%7D%0A%20%0A%2F%2F%20Function%202%0Aint%20fun2(int%20arr%5BR%5D%5BC%5D)%0A%7B%0A%20%20%20%20int%20sum%20%3D%200%3B%0A%20%20%20%20for%20(int%20j%3D0%3B%20j%3CC%3B%20j%2B%2B)%0A%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3CR%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20sum%20%2B%3D%20arr%5Bi%5D%5Bj%5D%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}