{"id":3144,"date":"2019-10-09T11:13:11","date_gmt":"2019-10-09T11:13:11","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=3144"},"modified":"2024-02-16T10:16:35","modified_gmt":"2024-02-16T10:16:35","slug":"compiler-optimization","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=3144","title":{"rendered":"Compiler optimization"},"content":{"rendered":"<p>A compiler with enabled optimization (via flags) will take more time for the generation, but the execution of generated binary will most likely have a better performance.<\/p>\n<p><u>Hint:<\/u> If you are as lazy as I am you can use a web tool (<a href=\"https:\/\/godbolt.org\/\">https:\/\/godbolt.org\/<\/a>) to take a look on the generated assembly code for a choosen compiler.<\/p>\n<hr>\n<p><strong>Examples<\/strong><br \/>\nFor the following examples I have choosen GCC 9.2 (x86-64) as compiler.<\/p>\n<p><u>Useless stuff removal<\/u><\/p>\n<table>\n<col width=\"25%\">\n<col width=\"45%\">\n<col width=\"30%\">\n<tr>\n<th>Source code<\/th>\n<th>Without optimization<\/th>\n<th>With optimization (-O)<\/th>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n      void function()\r\n      {\r\n        int i;\r\n        i = 42;\r\n      }\r\n      <\/pre>\n<\/td>\n<td valign=\"top\">\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n      function():\r\n        push  rbp\r\n        mov   rbp, rsp\r\n        mov   DWORD PTR [rbp-4], 42\r\n        nop\r\n        pop   rbp\r\n        ret\r\n      <\/pre>\n<\/td>\n<td valign=\"top\">\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n      function():\r\n        ret\r\n      <\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p><u>Pre-calculation<\/u><\/p>\n<table>\n<col width=\"25%\">\n<col width=\"45%\">\n<col width=\"30%\">\n<tr>\n<th>Source code<\/th>\n<th>Without optimization<\/th>\n<th>With optimization (-O)<\/th>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n      int function()\r\n      {\r\n        int i = 0;\r\n        i++;\r\n        return i;\r\n      }\r\n      <\/pre>\n<\/td>\n<td valign=\"top\">\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n      function():\r\n        push  rbp\r\n        mov   rbp, rsp\r\n        mov   DWORD PTR [rbp-4], 0\r\n        add   DWORD PTR [rbp-4], 1\r\n        mov   eax, DWORD PTR [rbp-4]\r\n        pop   rbp\r\n        ret\r\n      <\/pre>\n<\/td>\n<td valign=\"top\">\n<pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\r\n      function():\r\n        mov   eax, 1\r\n        ret\r\n      <\/pre>\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>A compiler with enabled optimization (via flags) will take more time for the generation, but the execution of generated binary<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[39],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3144"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3144"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3144\/revisions"}],"predecessor-version":[{"id":16714,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3144\/revisions\/16714"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}