{"id":13,"date":"2013-12-22T02:02:39","date_gmt":"2013-12-22T02:02:39","guid":{"rendered":"http:\/\/codingforspeed.com\/?p=13"},"modified":"2017-02-03T15:27:32","modified_gmt":"2017-02-03T15:27:32","slug":"unrolling-the-loop","status":"publish","type":"post","link":"https:\/\/codingforspeed.com\/unrolling-the-loop\/","title":{"rendered":"Unrolling the Loop"},"content":{"rendered":"\n<div id=\"fb-root\"><\/div>\n<script>(function(d, s, id) {\n  var js, fjs = d.getElementsByTagName(s)[0];\n  if (d.getElementById(id)) return;\n  js = d.createElement(s); js.id = id;\n  js.src = \"\/\/connect.facebook.net\/en_GB\/sdk.js#xfbml=1&version=v2.6&appId=857807644244147\";\n  fjs.parentNode.insertBefore(js, fjs);\n}(document, 'script', 'facebook-jssdk'));<\/script>\n\n<div class=\"fb-like\" data-href=\"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts\/13\" data-layout=\"button_count\" data-action=\"like\" data-show-faces=\"false\" data-share=\"false\"><\/div><BR\/><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<!-- coding-wp-top -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-5311181282559050\"\n     data-ad-slot=\"4369234259\"\n     data-ad-format=\"auto\"><\/ins>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><BR\/><div class='wp_content'><p>Unrolling the Loop can be considered the simple-but-effective optimisation method.<\/p>\n<p>Usually, the operations done in a loop can be grouped into two or more. In this case, unrolling them speeds up the code.<\/p>\n<p>For example, suppose you want to update the elements at even index to its plus one but decrement the elements at odd index by one, instead of having code like this:<\/p>\n<pre lang=\"c\">for (int i = 0; i &lt;= 100; i ++) {\r\n  if (i &amp; 1 == 0) {\r\n    arr[i] ++;\r\n  } else {\r\n    arr[i] --;\r\n  }\r\n}<\/pre>\n<p>You should unrolling the loop like this:<\/p>\n<pre lang=\"c\">for (int i = 0; i &lt;= 100; i +2) {\r\n  arr[i] ++;\r\n}\r\n\r\nfor (int i = 1; i &lt;= 100; i +2) {\r\n  arr[i] --;\r\n}<\/pre>\n<p>The loop unrolling is faster because unnecessary if-checks are avoided. The branching is costly especially if mis-predicted by the processors.<\/p>\n<p>Loop unrolling sometimes can be referred to removing for-loops for known small loops. For example,<\/p>\n<pre lang=\"c\">for (int i = 0; i &lt; 4; i ++) {\r\n  arr[i] = i;\r\n}<\/pre>\n<p>should be changed to:<\/p>\n<pre lang=\"c\">arr[0] = 0;\r\narr[1] = 1;\r\narr[2] = 2;\r\narr[3] = 3;<\/pre>\n<p>This can be useful in multiplication of small-size matrix, where sizes of both matrix are known e.g. 2&#215;2 times 2&#215;4. In this case, unrolling the loop and multiply each element is a lot faster that removes the unnecessary loop (condition jumping)<\/p>\n<p>Why do we need to unroll the loop? In a short word, to avoid condition mis-predict and cache miss due to condition checks in a loop.<\/p>\n<p>&#8211;EOF (<a  href=\"https:\/\/codingforspeed.com\">Coding For Speed<\/a>) &#8212; <\/p>\n\n<div class=\"fb-like\" data-href=\"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts\/13\" data-layout=\"button_count\" data-action=\"like\" data-show-faces=\"false\" data-share=\"false\"><\/div>  <font color=gray>271 words<\/font>\n\n<BR\/><\/div> The Permanent URL is: <a href=\"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts\/13\">Unrolling the Loop<\/a> <a href=\"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts\/13\/amp\/\">(<B>AMP Version<\/B>)<\/a><BR\/> <div class=\"addthis_toolbox addthis_default_style addthis_32x32_style\">\n<a class=\"addthis_button_facebook\"><\/a>\n<a class=\"addthis_button_twitter\"><\/a>\n<a class=\"addthis_button_linkedin\"><\/a>\n<a class=\"addthis_button_reddit\"><\/a>\n<a class=\"addthis_button_email\"><\/a>\n<\/div><BR\/>","protected":false},"excerpt":{"rendered":"<p>Unrolling the Loop can be considered the simple-but-effective optimisation method. Usually, the operations done in a loop can be grouped into two or more. In this case, unrolling them speeds up the code. For example, suppose you want to update the elements at even index to its plus one but decrement the elements at odd index by one, instead of having code like this: for (int i = 0; i &lt;= 100; i ++) { if (i &amp; 1 == 0) { arr ++; } else { arr &#8211;; } } You should unrolling the loop like this: for (int<span class=\"continue-reading\"> <a href=\"https:\/\/codingforspeed.com\/unrolling-the-loop\/\"><B>Continue Reading<\/B> &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":238,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,83,3],"tags":[7,13,6],"class_list":["post-13","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-array","category-cc","category-general","tag-cc","tag-cache-hit","tag-loop","has_thumb"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.8 - aioseo.com -->\n\t<meta name=\"description\" content=\"Unrolling the Loop, general optimisation\" \/>\n\t<meta name=\"robots\" content=\"max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n\t<meta name=\"author\" content=\"ACMer\"\/>\n\t<meta name=\"keywords\" content=\"unrolling the loop,general optimisation,c\/c++,cache hit,loop,array,general\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/codingforspeed.com\/unrolling-the-loop\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.8\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#article\",\"name\":\"Unrolling the Loop | CodingForSpeed.COM\",\"headline\":\"Unrolling the Loop\",\"author\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/author\\\/acmer\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/codingforspeed.com\\\/wp-content\\\/uploads\\\/2014\\\/12\\\/c-n-c.jpg\",\"width\":800,\"height\":600,\"caption\":\"C\\\/C++ Programming\"},\"datePublished\":\"2013-12-22T02:02:39+00:00\",\"dateModified\":\"2017-02-03T15:27:32+00:00\",\"inLanguage\":\"en-US.UTF-8\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#webpage\"},\"articleSection\":\"array, C\\\/C++, general, c\\\/c++, cache hit, loop\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/codingforspeed.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/category\\\/general\\\/#listItem\",\"name\":\"general\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/category\\\/general\\\/#listItem\",\"position\":2,\"name\":\"general\",\"item\":\"https:\\\/\\\/codingforspeed.com\\\/category\\\/general\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#listItem\",\"name\":\"Unrolling the Loop\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#listItem\",\"position\":3,\"name\":\"Unrolling the Loop\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/category\\\/general\\\/#listItem\",\"name\":\"general\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/#person\",\"name\":\"ACMer\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fdd6f711767535d8c10ab18e7b9a0dfc387ea095390f89a89b21ff0df33863c5?s=96&d=blank&r=g\",\"width\":96,\"height\":96,\"caption\":\"ACMer\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/author\\\/acmer\\\/#author\",\"url\":\"https:\\\/\\\/codingforspeed.com\\\/author\\\/acmer\\\/\",\"name\":\"ACMer\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fdd6f711767535d8c10ab18e7b9a0dfc387ea095390f89a89b21ff0df33863c5?s=96&d=blank&r=g\",\"width\":96,\"height\":96,\"caption\":\"ACMer\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#webpage\",\"url\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/\",\"name\":\"Unrolling the Loop | CodingForSpeed.COM\",\"description\":\"Unrolling the Loop, general optimisation\",\"inLanguage\":\"en-US.UTF-8\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/author\\\/acmer\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/author\\\/acmer\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/codingforspeed.com\\\/wp-content\\\/uploads\\\/2014\\\/12\\\/c-n-c.jpg\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#mainImage\",\"width\":800,\"height\":600,\"caption\":\"C\\\/C++ Programming\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/unrolling-the-loop\\\/#mainImage\"},\"datePublished\":\"2013-12-22T02:02:39+00:00\",\"dateModified\":\"2017-02-03T15:27:32+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/#website\",\"url\":\"https:\\\/\\\/codingforspeed.com\\\/\",\"name\":\"Code Optimisation Tips, Coding For Speed, Programming Tips\",\"alternateName\":\"Make Code Faster - Coding Tips\",\"description\":\"Don't Waste 1 CPU Cycle or 1 Byte\",\"inLanguage\":\"en-US.UTF-8\",\"publisher\":{\"@id\":\"https:\\\/\\\/codingforspeed.com\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Unrolling the Loop | CodingForSpeed.COM","description":"Unrolling the Loop, general optimisation","canonical_url":"https:\/\/codingforspeed.com\/unrolling-the-loop\/","robots":"max-snippet:-1, max-image-preview:large, max-video-preview:-1","keywords":"unrolling the loop,general optimisation,c\/c++,cache hit,loop,array,general","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#article","name":"Unrolling the Loop | CodingForSpeed.COM","headline":"Unrolling the Loop","author":{"@id":"https:\/\/codingforspeed.com\/author\/acmer\/#author"},"publisher":{"@id":"https:\/\/codingforspeed.com\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/codingforspeed.com\/wp-content\/uploads\/2014\/12\/c-n-c.jpg","width":800,"height":600,"caption":"C\/C++ Programming"},"datePublished":"2013-12-22T02:02:39+00:00","dateModified":"2017-02-03T15:27:32+00:00","inLanguage":"en-US.UTF-8","mainEntityOfPage":{"@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#webpage"},"isPartOf":{"@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#webpage"},"articleSection":"array, C\/C++, general, c\/c++, cache hit, loop"},{"@type":"BreadcrumbList","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/codingforspeed.com#listItem","position":1,"name":"Home","item":"https:\/\/codingforspeed.com","nextItem":{"@type":"ListItem","@id":"https:\/\/codingforspeed.com\/category\/general\/#listItem","name":"general"}},{"@type":"ListItem","@id":"https:\/\/codingforspeed.com\/category\/general\/#listItem","position":2,"name":"general","item":"https:\/\/codingforspeed.com\/category\/general\/","nextItem":{"@type":"ListItem","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#listItem","name":"Unrolling the Loop"},"previousItem":{"@type":"ListItem","@id":"https:\/\/codingforspeed.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#listItem","position":3,"name":"Unrolling the Loop","previousItem":{"@type":"ListItem","@id":"https:\/\/codingforspeed.com\/category\/general\/#listItem","name":"general"}}]},{"@type":"Person","@id":"https:\/\/codingforspeed.com\/#person","name":"ACMer","image":{"@type":"ImageObject","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/fdd6f711767535d8c10ab18e7b9a0dfc387ea095390f89a89b21ff0df33863c5?s=96&d=blank&r=g","width":96,"height":96,"caption":"ACMer"}},{"@type":"Person","@id":"https:\/\/codingforspeed.com\/author\/acmer\/#author","url":"https:\/\/codingforspeed.com\/author\/acmer\/","name":"ACMer","image":{"@type":"ImageObject","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/fdd6f711767535d8c10ab18e7b9a0dfc387ea095390f89a89b21ff0df33863c5?s=96&d=blank&r=g","width":96,"height":96,"caption":"ACMer"}},{"@type":"WebPage","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#webpage","url":"https:\/\/codingforspeed.com\/unrolling-the-loop\/","name":"Unrolling the Loop | CodingForSpeed.COM","description":"Unrolling the Loop, general optimisation","inLanguage":"en-US.UTF-8","isPartOf":{"@id":"https:\/\/codingforspeed.com\/#website"},"breadcrumb":{"@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#breadcrumblist"},"author":{"@id":"https:\/\/codingforspeed.com\/author\/acmer\/#author"},"creator":{"@id":"https:\/\/codingforspeed.com\/author\/acmer\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/codingforspeed.com\/wp-content\/uploads\/2014\/12\/c-n-c.jpg","@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#mainImage","width":800,"height":600,"caption":"C\/C++ Programming"},"primaryImageOfPage":{"@id":"https:\/\/codingforspeed.com\/unrolling-the-loop\/#mainImage"},"datePublished":"2013-12-22T02:02:39+00:00","dateModified":"2017-02-03T15:27:32+00:00"},{"@type":"WebSite","@id":"https:\/\/codingforspeed.com\/#website","url":"https:\/\/codingforspeed.com\/","name":"Code Optimisation Tips, Coding For Speed, Programming Tips","alternateName":"Make Code Faster - Coding Tips","description":"Don't Waste 1 CPU Cycle or 1 Byte","inLanguage":"en-US.UTF-8","publisher":{"@id":"https:\/\/codingforspeed.com\/#person"}}]}},"aioseo_meta_data":{"post_id":"13","title":"Unrolling the Loop | #site_title","description":"Unrolling the Loop, general optimisation","keywords":[{"label":"Unrolling the Loop","value":"Unrolling the Loop"},{"label":"general optimisation","value":"general optimisation"}],"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2020-12-22 12:38:39","updated":"2025-06-05 17:42:57","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codingforspeed.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/codingforspeed.com\/category\/general\/\" title=\"general\">general<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUnrolling the Loop\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/codingforspeed.com"},{"label":"general","link":"https:\/\/codingforspeed.com\/category\/general\/"},{"label":"Unrolling the Loop","link":"https:\/\/codingforspeed.com\/unrolling-the-loop\/"}],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":0,"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/media\/238"}],"wp:attachment":[{"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingforspeed.com\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}