{"id":20279,"date":"2022-11-09T06:10:52","date_gmt":"2022-11-09T00:40:52","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/"},"modified":"2022-11-09T06:10:52","modified_gmt":"2022-11-09T00:40:52","slug":"solved-if-else-if-ladder-and-compiler-optimization","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/","title":{"rendered":"[Solved] if-else if ladder and Compiler Optimization"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-28518085\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"28518085\" data-parentid=\"28517041\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>A large portion of this question will depend on what <code>A<\/code>, <code>B<\/code> and <code>C<\/code> really are (and the compiler will optimise it, as shown below). Simple types, definitely not worth worrying about. If they are some kind of &#8220;big number math&#8221; objects, or some complicated data type that needs 1000 instructions for each &#8220;is this <code>true<\/code> or not&#8221;, then there will be a big difference if the compiler decides to make different code.<\/p>\n<p><strong>As always when it comes to performance: Measure in your own code, use profiling to detect where the code spends MOST of the time, and then measure with changes to that code. Repeat until it runs fast enough [whatever that is] and\/or your manager tells you to stop fiddling with the code. Typically, however, unless it&#8217;s REALLY a high traffic area of the code, it will make little difference to re-arrange the conditions in an if-statement, it is the overall algorithm that makes most impact in the general case.<\/strong><\/p>\n<p>If we assume A, B and C are simple types, such as <code>int<\/code>, we can write some code to investigate:<\/p>\n<pre><code>extern int A, B, C;\nextern void UpdateData();\nextern void ResetData();\n\nvoid func1()\n{\n    if ( A &amp;&amp; B &amp;&amp; C ) {\n        UpdateData();\n    } else if ( A &amp;&amp; B ){\n        ResetData();\n    }\n}\n\n\nvoid func2()\n{\n    if ( A &amp;&amp; B) {\n        if (C) {\n            UpdateData();\n        } else {\n            ResetData();\n        }\n    }\n}\n<\/code><\/pre>\n<p>gcc 4.8.2 given this, with -O1 produces this code:<\/p>\n<pre><code>_Z5func1v:\n    cmpl    $0, A(%rip)\n    je  .L6\n    cmpl    $0, B(%rip)\n    je  .L6\n    subq    $8, %rsp\n    cmpl    $0, C(%rip)\n    je  .L3\n    call    _Z10UpdateDatav\n    jmp .L1\n.L3:\n    call    _Z9ResetDatav\n.L1:\n    addq    $8, %rsp\n.L6:\n    rep ret\n\n_Z5func2v:\n.LFB1:\n    cmpl    $0, A(%rip)\n    je  .L12\n    cmpl    $0, B(%rip)\n    je  .L12\n    subq    $8, %rsp\n    cmpl    $0, C(%rip)\n    je  .L9\n    call    _Z10UpdateDatav\n    jmp .L7\n.L9:\n    call    _Z9ResetDatav\n.L7:\n    addq    $8, %rsp\n.L12:\n    rep ret\n<\/code><\/pre>\n<p><em>In other words: No difference at all<\/em><\/p>\n<p>Using clang++ 3.7 (as of about 3 weeks ago) with -O1 gives this:<\/p>\n<pre><code>_Z5func1v:                              # @_Z5func1v\n    cmpl    $0, A(%rip)\n    setne   %cl\n    cmpl    $0, B(%rip)\n    setne   %al\n    andb    %cl, %al\n    movzbl  %al, %ecx\n    cmpl    $1, %ecx\n    jne .LBB0_2\n    movl    C(%rip), %ecx\n    testl   %ecx, %ecx\n    je  .LBB0_2\n    jmp _Z10UpdateDatav         # TAILCALL\n.LBB0_2:                                # %if.else\n    testb   %al, %al\n    je  .LBB0_3\n    jmp _Z9ResetDatav           # TAILCALL\n.LBB0_3:                                # %if.end8\n    retq\n\n_Z5func2v:                              # @_Z5func2v\n    cmpl    $0, A(%rip)\n    je  .LBB1_4\n    movl    B(%rip), %eax\n    testl   %eax, %eax\n    je  .LBB1_4\n    cmpl    $0, C(%rip)\n    je  .LBB1_3\n    jmp _Z10UpdateDatav         # TAILCALL\n.LBB1_4:                                # %if.end4\n    retq\n.LBB1_3:                                # %if.else\n    jmp _Z9ResetDatav           # TAILCALL\n.Ltmp1:\n<\/code><\/pre>\n<p>The chaining of and in the func1 of clang MAY be of benefit, but it&#8217;s probably such a small difference that you should concentrate on what makes more sense from a logical perspective of the code.<\/p>\n<p><strong>In summary: Not worth it<\/strong><\/p>\n<p>Higher optimisation in g++ makes it do the same tailcall optimisation that clang does, otherwise no difference.<\/p>\n<p>However, if we make <code>A<\/code>, <code>B<\/code> and <code>C<\/code> into external functions, which the compiler can&#8217;t &#8220;understand&#8221;, then we get a difference:<\/p>\n<pre><code>_Z5func1v:                              # @_Z5func1v\n    pushq   %rax\n.Ltmp0:\n    .cfi_def_cfa_offset 16\n    callq   _Z1Av\n    testl   %eax, %eax\n    je  .LBB0_3\n\n    callq   _Z1Bv\n    testl   %eax, %eax\n    je  .LBB0_3\n\n    callq   _Z1Cv\n    testl   %eax, %eax\n    je  .LBB0_3\n\n    popq    %rax\n    jmp _Z10UpdateDatav         # TAILCALL\n.LBB0_3:                                # %if.else\n    callq   _Z1Av\n    testl   %eax, %eax\n    je  .LBB0_5\n\n    callq   _Z1Bv\n    testl   %eax, %eax\n    je  .LBB0_5\n\n    popq    %rax\n    jmp _Z9ResetDatav           # TAILCALL\n.LBB0_5:                                # %if.end12\n    popq    %rax\n    retq\n\n_Z5func2v:                              # @_Z5func2v\n    pushq   %rax\n.Ltmp2:\n    .cfi_def_cfa_offset 16\n    callq   _Z1Av\n    testl   %eax, %eax\n    je  .LBB1_4\n\n    callq   _Z1Bv\n    testl   %eax, %eax\n    je  .LBB1_4\n\n    callq   _Z1Cv\n    testl   %eax, %eax\n    je  .LBB1_3\n\n    popq    %rax\n    jmp _Z10UpdateDatav         # TAILCALL\n.LBB1_4:                                # %if.end6\n    popq    %rax\n    retq\n.LBB1_3:                                # %if.else\n    popq    %rax\n    jmp _Z9ResetDatav           # TAILCALL\n<\/code><\/pre>\n<p>Here we DO see the difference between <code>func1<\/code> and <code>func2<\/code>, where <code>func1<\/code> will call <code>A<\/code> and <code>B<\/code> twice &#8211; since the compiler can&#8217;t assume that calling those functions ONCE will do the same thing as calling twice. [Consider that the functions <code>A<\/code> and <code>B<\/code> may be reading data from a file, calling <code>rand<\/code>, or whatever, the result of NOT calling that function may be that the program behaves differently.<\/p>\n<p>(In this case I only posted clang code, but g++ produces code that has the same outcome, but slightly different ordering of the different lumps of code)<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved if-else if ladder and Compiler Optimization <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] A large portion of this question will depend on what A, B and C really are (and the compiler will optimise it, as shown below). Simple types, definitely not worth worrying about. If they are some kind of &#8220;big number math&#8221; objects, or some complicated data type that needs 1000 instructions for each &#8220;is &#8230; <a title=\"[Solved] if-else if ladder and Compiler Optimization\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/\" aria-label=\"More on [Solved] if-else if ladder and Compiler Optimization\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,608],"class_list":["post-20279","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-gcc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] if-else if ladder and Compiler Optimization - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] if-else if ladder and Compiler Optimization - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] A large portion of this question will depend on what A, B and C really are (and the compiler will optimise it, as shown below). Simple types, definitely not worth worrying about. If they are some kind of &#8220;big number math&#8221; objects, or some complicated data type that needs 1000 instructions for each &#8220;is ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-09T00:40:52+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] if-else if ladder and Compiler Optimization\",\"datePublished\":\"2022-11-09T00:40:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/\"},\"wordCount\":387,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"c++\",\"gcc\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/\",\"name\":\"[Solved] if-else if ladder and Compiler Optimization - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-11-09T00:40:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-if-else-if-ladder-and-compiler-optimization\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] if-else if ladder and Compiler Optimization\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] if-else if ladder and Compiler Optimization - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] if-else if ladder and Compiler Optimization - JassWeb","og_description":"[ad_1] A large portion of this question will depend on what A, B and C really are (and the compiler will optimise it, as shown below). Simple types, definitely not worth worrying about. If they are some kind of &#8220;big number math&#8221; objects, or some complicated data type that needs 1000 instructions for each &#8220;is ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/","og_site_name":"JassWeb","article_published_time":"2022-11-09T00:40:52+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] if-else if ladder and Compiler Optimization","datePublished":"2022-11-09T00:40:52+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/"},"wordCount":387,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","gcc"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/","url":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/","name":"[Solved] if-else if ladder and Compiler Optimization - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-09T00:40:52+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-if-else-if-ladder-and-compiler-optimization\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] if-else if ladder and Compiler Optimization"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777613206","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20279"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20279\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}