{"id":5547,"date":"2014-03-31T00:36:12","date_gmt":"2014-03-31T00:36:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/31\/why-comparing-char-varibles-twice-is-faster-than-comparing-short-varibles-once-collection-of-common-programming-errors\/"},"modified":"2014-03-31T00:36:12","modified_gmt":"2014-03-31T00:36:12","slug":"why-comparing-char-varibles-twice-is-faster-than-comparing-short-varibles-once-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/31\/why-comparing-char-varibles-twice-is-faster-than-comparing-short-varibles-once-collection-of-common-programming-errors\/","title":{"rendered":"why comparing char varibles twice is faster than comparing short varibles once-Collection of common programming errors"},"content":{"rendered":"<p>I have thought one compare must be faster than two. But after my test, I found in debug mode short compare is a bit faster, and in release mode char compare is faster. And I want to know the true reason.<\/p>\n<p>Following is the test code and test result. I wrote two simple function, func1 using two char compare, func2 using one short compare. The main funcion returns temporary return value to avoid compile optimization ignoring my test code. My compiler is GCC 4.7.2, CPU Intel(R) Xeon(R) CPU E5-2430 0 @ 2.20GHz (VM).<\/p>\n<pre><code>inline int func1(unsigned char word[2])\n{\n        if (word[0] == 0xff &amp;&amp; word[1] == 0xff)\n                return 1;\n        return 0;\n}\n\n\ninline int func2(unsigned char word[2])\n{\n        if (*(unsigned short*)word == 0xffff)\n                return 1;\n        return 0;\n}\n\nint main()\n{\n        int n_ret = 0;\n        for (int j = 0; j &lt; 10000; ++j)\n                for (int i = 0; i &lt; 70000; ++i)\n                        n_ret += func2((unsigned char*)&amp;i);\n        return n_ret;\n}\n<\/code><\/pre>\n<p>Debug mode:<\/p>\n<pre><code>          func1      func2\nreal    0m3.621s    0m3.586s\nuser    0m3.614s    0m3.579s\nsys     0m0.001s    0m0.000s\n<\/code><\/pre>\n<p>Release mode:<\/p>\n<pre><code>          func1      func2\nreal    0m0.833s    0m0.880s\nuser    0m0.831s    0m0.878s\nsys     0m0.000s    0m0.002s\n<\/code><\/pre>\n<p>func1 edition&#8217;s assemblly code:<\/p>\n<pre><code>        .cfi_startproc\n        movl    $10000, %esi\n        xorl    %eax, %eax\n        .p2align 4,,10\n        .p2align 3\n.L6:\n        movl    $1, %edx\n        xorl    %ecx, %ecx\n        .p2align 4,,10\n        .p2align 3\n.L8:\n        movl    %edx, -24(%rsp)\n        addl    $1, %edx\n        addl    %ecx, %eax\n        cmpl    $70001, %edx\n        je      .L3\n        xorl    %ecx, %ecx\n        cmpb    $-1, -24(%rsp)\n        jne     .L8\n        xorl    %ecx, %ecx\n        cmpb    $-1, -23(%rsp)\n        sete    %cl\n        jmp     .L8\n        .p2align 4,,10\n        .p2align 3\n.L3:\n        subl    $1, %esi\n        jne     .L6\n        rep\n        ret\n        .cfi_endproc\n<\/code><\/pre>\n<p>func2 edition&#8217;s assemblly code:<\/p>\n<pre><code>        .cfi_startproc\n        movl    $10000, %esi\n        xorl    %eax, %eax\n        .p2align 4,,10\n        .p2align 3\n.L4:\n        movl    $1, %edx\n        xorl    %ecx, %ecx\n        jmp     .L3\n        .p2align 4,,10\n        .p2align 3\n.L7:\n        movzwl  -24(%rsp), %ecx\n.L3:\n        cmpw    $-1, %cx\n        movl    %edx, -24(%rsp)\n        sete    %cl\n        addl    $1, %edx\n        movzbl  %cl, %ecx\n        addl    %ecx, %eax\n        cmpl    $70001, %edx\n        jne     .L7\n        subl    $1, %esi\n        jne     .L4\n        rep\n        ret\n        .cfi_endproc\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have thought one compare must be faster than two. But after my test, I found in debug mode short compare is a bit faster, and in release mode char compare is faster. And I want to know the true reason. Following is the test code and test result. I wrote two simple function, func1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5547","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5547"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5547\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}