dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: What's faster, if statement or assignment?

  1. #1
    Join Date
    Jul 2007
    Posts
    609

    What's faster, if statement or assignment?

    Say I have something like this, which one executes faster?

    Code:
    if(m_submitmode)m_submitmode=false;
    OR

    Code:
    m_submitmode=false;
    This is assuming m_submitmode will not always be true, but I want it to be false at that point. Is it faster to just assign it false, or should I be checking first? Now if it is true, the first statement will be slower as it has to do two things, but if it's already false, will the first one be faster?

    This is extremely small time we are talking about and wont really make a difference on a small or even medium scale but it's one of those things that is fairly easy to do so may as well pick the faster route.
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What's faster, if statement or assignment?

    Quote Originally Posted by Red Squirrel View Post
    Say I have something like this, which one executes faster?
    You are assuming that what you write in source form is exactly what will be executed. The problem is that the compiler will optimize code, possibly removing the if() altogether, making the question moot.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: What's faster, if statement or assignment?

    IMO, if the variable needs to be false and it doesn't matter whether it's currently true, just set it to false. I can't possibly see the difference (if any) being enough to worry about.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What's faster, if statement or assignment?

    Quote Originally Posted by Red Squirrel View Post
    Say I have something like this, which one executes faster?
    To add, the best way to answer your question is to ask you "if you wrote the compiler, which one would be faster?". Then you have control over all the rules, which lines of code will be executed, what instruction set you want to use, whether something is optimized etc. Otherwise, the original answer I gave stands. You can't judge which is faster, since a real compiler optimizes code in all sorts of ways, all dependent on the compiler itself.

    Which is "faster" and "slower" is more appropriately asked when dealing with algorithms or storage concerns. For example, questions like "is a merge sort faster than a quicksort", or "is using a vector faster than using a set", or some other such things similar to this.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2007
    Posts
    609

    Re: What's faster, if statement or assignment?

    So guess I should just set it and be done with it then. Save a few characters of code. :P
    http://www.uovalor.com :: Free UO Server

  6. #6
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: What's faster, if statement or assignment?

    Although Paul is right in saying compiler optimizes the code, I think some code constructs are costly by their own nature.

    If (<some var>)
    <some var> = false

    This construct will always be costlier irrespective of the value of <some var> than <some var> = false because if introduces a 'jump' instruction in assembly. And jump does all bad things to performance like stalling the instruction pipeline for a few cycles till the new address is fetched.

  7. #7
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: What's faster, if statement or assignment?

    Oops, I forgot about conditional instructions on ARM in which case 'if' may not compile into a jump but a conditional instruction which will take the same time as setting it to false.

  8. #8
    Join Date
    Nov 2008
    Posts
    1

    Smile Re: What's faster, if statement or assignment?

    Rohshall,

    You make a very good point. In today's world of modern, optimizing compilers, it is all to easy to forget what is going on "under the hood." I have always been a strong proponent of clean, efficient coding decisions. And here's another point that can be made:

    If a certain variable is going to be set to a value of false under all circumstances (i.e., whether or not its current value is true _or_ false), it makes better sense intuitively to simply set the value of the variable, rather than clouding actual intentions by introducing an (unnecessary) if statement. Example:

    myvar = false; // set myvar to false

    will usually be easier to understand than in

    if (myvar) {
    myvar = false; // arguably the same outcome, but...
    }

    And you're point about the costly jmp instruction is well taken, too. Even in these days of fast processors and seemingly limitless memory, there is still something to be said for good design decisions.

  9. #9
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: What's faster, if statement or assignment?

    I agree with RFIDtag30 on this - it is better to write code that is easier to understand. If all you want is to set a particular variable to a preset value then do that.

    For example, on some microcontrollers it is shorter and faster to subtract a register from itself, rather than setting it to zero. Still in C I would write SomeVariable = 0; rather than SomeVariable -= SomeVariable; as I expect the optimiser to use the best possible version that does the job. So I do not have to sacrifice readability and the beast of maintanability. In the end this is just a form of premature optimalization.

    The only time where I would consider the difference between the two statements is when SomeVariable is an object rather than a simple data type. For example if it is an object where the operator= implementation is a lot more CPU intensive compared to the operator== implementation. Then I would of course also add a comment that explains why it is done that way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)