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 371751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:08:40+00:00 2026-05-12T14:08:40+00:00

Is there a performance difference between the following two pieces of code? if (myCondition)

  • 0

Is there a performance difference between the following two pieces of code?

if (myCondition)
{
     return "returnVal1";
}

return "returnVal2"

and

if (myCondition)
{
     return "returnVal1";
}
else
{
     return "returnVal2";
}

My gut feeling is that the compiler should optimize for this and there shouldn’t be a difference, but I frequently see it done both ways throughout our code. I’d like to know if it comes down to a matter of preference and readability.

  • 1 1 Answer
  • 4 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-12T14:08:40+00:00Added an answer on May 12, 2026 at 2:08 pm

    The best way to find out is to look at the code! Here’s the code the VS2005 C# produced in release mode:

        static bool F1 (int condition)
        {
          if (condition > 100)
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        eax  
    00000004  mov         dword ptr [ebp-4],ecx 
    00000007  cmp         dword ptr ds:[009185C8h],0 
    0000000e  je          00000015 
    00000010  call        79469149 
    00000015  cmp         dword ptr [ebp-4],64h 
    00000019  jle         00000024 
          {
            return true;
    0000001b  mov         eax,1 
    00000020  mov         esp,ebp 
    00000022  pop         ebp  
    00000023  ret              
          }
    
          return false;
    00000024  xor         eax,eax 
    00000026  mov         esp,ebp 
    00000028  pop         ebp  
    00000029  ret              
                }
    
        static bool F2 (int condition)
        {
          if (condition > 100)
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        eax  
    00000004  mov         dword ptr [ebp-4],ecx 
    00000007  cmp         dword ptr ds:[009185C8h],0 
    0000000e  je          00000015 
    00000010  call        79469109 
    00000015  cmp         dword ptr [ebp-4],64h 
    00000019  jle         00000024 
          {
            return true;
    0000001b  mov         eax,1 
    00000020  mov         esp,ebp 
    00000022  pop         ebp  
    00000023  ret              
          }
          else
          {
            return false;
    00000024  xor         eax,eax 
    00000026  mov         esp,ebp 
    00000028  pop         ebp  
    00000029  ret              
                }
    

    Which shows the two version produce the exact same code, as you would hope for. I also tried a third option:

        static bool F3 (int condition)
        {
          return condition > 100;
    00000000  push        ebp  
    00000001  mov         ebp,esp 
    00000003  push        eax  
    00000004  mov         dword ptr [ebp-4],ecx 
    00000007  cmp         dword ptr ds:[009185C8h],0 
    0000000e  je          00000015 
    00000010  call        794690C9 
    00000015  cmp         dword ptr [ebp-4],64h 
    00000019  setg        al   
    0000001c  movzx       eax,al 
    0000001f  mov         esp,ebp 
    00000021  pop         ebp  
    00000022  ret              
                }
    

    which is far more efficient as it never branches (and branches are usually bad!).

    EDIT

    Actually, the best way to find out which is more efficient is to profile the code, not look at the assembler.

    Also, the code it’s produced is quite unusual. The push eax / mov [],ecx bit is the same, surely, as a single push ecx. Also, it passes by register then stores the value on the stack. I wonder if running the code in the debugger to look at the assembler is changing the way the code is generated.

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

Sidebar

Related Questions

Is there any performance difference between the following two statements? from item in collection
Is there any performance difference between the following two snippets? 1 IEnumerable<object> enumerable =
Is there any performance difference between the two following queries: SELECT foo,bar FROM table
I have been told that there is a performance difference between the following code
Is there a performance difference between the following 2 queries, and if so, then
I was wondering if there was any performance difference between the two approaches below.
Is there a major performance difference between the following: <iframe style=visibility:hidden /> <iframe style=width:0px;
Is there any difference(performance, implementation. .whatever) between the following: i) DllImport(kernel32.dll)] public extern static
When accessing values from an SqlDataReader is there a performance difference between these two:
Are there any differences when it comes to performance between the following three border

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.