Results 1 to 6 of 6

Thread: conditional operator good for optimization?

  1. #1
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default conditional operator good for optimization?

    I'm trying to remove branches from my code so it consumes less %cpu.

    If I convert an inner loop if statement from:

    Qt Code:
    1. if ( b ) {
    2. a = c;
    3. } else {
    4. a = d;
    5. }
    To copy to clipboard, switch view to plain text mode 

    To:

    Qt Code:
    1. a = b ? c : d
    To copy to clipboard, switch view to plain text mode 

    Does that take a branch out of the opcode?

    Initial tests indicate this does not reduce the %cpu of my app very much.
    Last edited by mhoover; 28th December 2010 at 01:11. Reason: formatting

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: conditional operator good for optimization?

    Its still a branch regardless of which way you put it, unless your target CPU supports conditional execution of opcodes, such as ARM (in which case, both still get the same result as the first could be done via conditional execution also).

    Sometimes operations can be done without branching, like the common r = (x < y) ? x : y can be done as r = y ^ ((x ^ y) & -(x < y)) but then you would have to check to ensure the extra intructions are faster than the branch in the previous code. Also, without a comment, you might not understand the second code is the same as the first, so for code maintainability, you should always use the former unless you have a real good reason to the later.

  3. The following user says thank you to squidge for this useful post:

    mhoover (28th December 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: conditional operator good for optimization?

    To add to what was already said, many architectures support branch prediction, i.e. ways of marking which of the branches is most likely to happen so that the resulting code and execution is better suited to the most common situation - that is if "b" is likely to be true, the processor can start loading code from the positive branch before the value of expression "b" is even evaluated.

    For example for gcc:
    Qt Code:
    1. #define likely(x) __builtin_expect((x),1)
    2. #define unlikely(x) __builtin_expect((x),0)
    3.  
    4. if(likely(b)) {
    5. a = c;
    6. } else {
    7. a = d;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    mhoover (28th December 2010)

  6. #4
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Thanks
    65
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: conditional operator good for optimization?

    Thanks squidge and wysota.

    I feel like I learned something.


    Added after 1 51 minutes:


    I found it interesting that the gcc websites say programmers are notoriously bad at predicting which branches are most likely to get called.

    I had a function that went through an enormous amount of pixels to find the highest and lowest intensities. I did something like:

    if (unlikely(pix_value > last_highest))
    last_highest = pix_value;

    And this increased my %cpu from 39 to 44! When I switched the "unlikely" to "likely" it dropped to about 37%. So I guess I am a poor judge.

    Later I removed the __builtin_expect stuff and used the -fprofile-arcs flag to get it down to 35 %cpu.
    Last edited by mhoover; 28th December 2010 at 21:00.

  7. #5
    Join Date
    Dec 2010
    Location
    US, Washington State
    Posts
    54
    Thanks
    3
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: conditional operator good for optimization?

    Quote Originally Posted by mhoover View Post
    I'm trying to remove branches from my code so it consumes less %cpu.
    Qt Code:
    1. if ( b ) {
    2. a = c;
    3. } else {
    4. a = d;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Preload variable a before the if statement -
    Qt Code:
    1. a = d;
    2. if(b)
    3. a = c;
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: conditional operator good for optimization?

    Quote Originally Posted by mhoover View Post
    I had a function that went through an enormous amount of pixels to find the highest and lowest intensities. I did something like:

    if (unlikely(pix_value > last_highest))
    last_highest = pix_value;
    Why would this be unlikely? Basically the probability that the next pixel has a higher value is equal to the probability it has a lower value. Using likely/unlikely is ok when you are testing a condition that you are sure is very likely or very unlikely to happen. If probabilities are more or less equal (say 40/60), there is no point in doing that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 7th October 2010, 20:47
  2. How Does One Put a Conditional For Embedded Linux into a .pro file?
    By cloaked_and_coastin in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 27th July 2010, 15:48
  3. Replies: 2
    Last Post: 1st March 2010, 17:58
  4. Conditional linkage
    By TorAn in forum General Programming
    Replies: 1
    Last Post: 22nd December 2009, 21:10
  5. QListView with conditional vertical automatic scroll
    By jmesquita in forum Qt Programming
    Replies: 2
    Last Post: 1st August 2009, 03:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.