Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:45:21+00:00 2026-05-26T12:45:21+00:00

I was just thinking is there any performance difference between the 2 statements in

  • 0

I was just thinking is there any performance difference between the 2 statements in C/C++:

Case 1:

if (p==0)
   do_this();
else if (p==1)
   do_that();
else if (p==2)
   do_these():

Case 2:

if(p==0)
    do_this();
if(p==1)
    do_that();
if(p==2)
    do_these();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:45:21+00:00Added an answer on May 26, 2026 at 12:45 pm

    Assuming simple types (in this case, I used int) and no funny business (didn’t redefine operator= for int), at least with GCC 4.6 on AMD64, there is no difference. The generated code is identical:

    0000000000000000 <case_1>:                                   0000000000000040 <case_2>:
       0:   85 ff                   test   %edi,%edi               40:   85 ff                   test   %edi,%edi
       2:   74 14                   je     18 <case_1+0x18>        42:   74 14                   je     58 <case_2+0x18>
       4:   83 ff 01                cmp    $0x1,%edi               44:   83 ff 01                cmp    $0x1,%edi
       7:   74 27                   je     30 <case_1+0x30>        47:   74 27                   je     70 <case_2+0x30>
       9:   83 ff 02                cmp    $0x2,%edi               49:   83 ff 02                cmp    $0x2,%edi
       c:   74 12                   je     20 <case_1+0x20>        4c:   74 12                   je     60 <case_2+0x20>
       e:   66 90                   xchg   %ax,%ax                 4e:   66 90                   xchg   %ax,%ax
      10:   f3 c3                   repz retq                      50:   f3 c3                   repz retq 
      12:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)        52:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)
      18:   31 c0                   xor    %eax,%eax               58:   31 c0                   xor    %eax,%eax
      1a:   e9 00 00 00 00          jmpq   1f <case_1+0x1f>        5a:   e9 00 00 00 00          jmpq   5f <case_2+0x1f>
      1f:   90                      nop                            5f:   90                      nop
      20:   31 c0                   xor    %eax,%eax               60:   31 c0                   xor    %eax,%eax
      22:   e9 00 00 00 00          jmpq   27 <case_1+0x27>        62:   e9 00 00 00 00          jmpq   67 <case_2+0x27>
      27:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)        67:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
      2e:   00 00                                                  6e:   00 00 
      30:   31 c0                   xor    %eax,%eax               70:   31 c0                   xor    %eax,%eax
      32:   e9 00 00 00 00          jmpq   37 <case_1+0x37>        72:   e9 00 00 00 00          jmpq   77 <case_2+0x37>
      37:   66 0f 1f 84 00 00 00    nopw   0x0(%rax,%rax,1)
      3e:   00 00 
    

    The extra instruction at the end of case_1 is just for padding (to get the next function aligned).

    This isn’t really surprising, figuring out that p isn’t changed in that function is fairly basic optimization. If p could be changed (e.g., passed-by-reference or pointer to the various do_… functions, or was a reference or pointer itself, so there could be an alias) then the behavior is different, and of course the generated code would be too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably just wishful thinking... Is there any way to check to see
Just looking at Python generators, real impressed with them, but are there any things
Just thinking about the best way to build an Order form that would (from
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
I just started thinking about creating/customizing a web crawler today, and know very little
Maybe I am just over thinking this and need to write some more prototype
Without thinking about it at all I just want to say I should allow
I've just started to learn C (using Thinking In C ) and I'm wondering
I have just done something without thinking it through. I have made a branch
This was just what I was thinking for this solution and I would like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.