Results 1 to 2 of 2

Thread: Questions to Q_LIKELY / Q_UNLIKELY

  1. #1
    Join Date
    Jul 2015
    Posts
    87
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Questions to Q_LIKELY / Q_UNLIKELY

    Hello,

    i have some questions to Q_LIKELY and Q_UNLIKEYLY.
    In Qt Docs they wrote it will help the compiler to optimize the code.

    Questions:
    1.) Does that really give an advantage in speed of my code? I found here in forum
    that also the CPU must support it and the benefit of it is not so much.

    2.) What if i have two expr. to evaluate

    Qt Code:
    1. if( expr1 || expr2 )
    2.  
    3. // Which one is prefered
    4. if( Q_LIKELY(exp1) || Q_LIKELY(expr2) ) // two Q_LIKELY
    5. or
    6. if( Q_LIKELY(exp1 || expr2) ) // in one Q_LIKELY
    To copy to clipboard, switch view to plain text mode 

    Thx
    Last edited by HappyCoder; 17th March 2016 at 10:30.

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

    Default Re: Questions to Q_LIKELY / Q_UNLIKELY

    Quote Originally Posted by HappyCoder View Post
    Hello,

    i have some questions to Q_LIKELY and Q_UNLIKEYLY.
    In Qt Docs they wrote it will help the compiler to optimize the code.

    Questions:
    1.) Does that really give an advantage in speed of my code? I found here in forum
    that also the CPU must support it and the benefit of it is not so much.
    It depends on the CPU architecture and how often such test is executed.

    2.) What if i have two expr. to evaluate

    Qt Code:
    1. if( expr1 || expr2 )
    2.  
    3. // Which one is prefered
    4. if( Q_LIKELY(exp1) || Q_LIKELY(expr2) ) // two Q_LIKELY
    5. or
    6. if( Q_LIKELY(exp1 || expr2) ) // in one Q_LIKELY
    To copy to clipboard, switch view to plain text mode 
    Basically you should (if semantically possible) put the more probable test first and wrap it in a likely statement (assuming it is likely the test succeeds). That will make the compiler prepare a branch instruction before executing the first test. Since the branch is likely, it makes little sense to set the second expression as likely. Unless of course you want to say that at least one of the tests is likely to pass (without determining which one), then the latter statement is better (however you will probably gain less by using it). So all in all, these are two different cases which can't be substituted by one another.
    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. Usage of Q_UNLIKELY and Q_LIKELY
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2014, 14:28
  2. questions
    By vimal in forum Qt Programming
    Replies: 3
    Last Post: 28th June 2012, 14:20
  3. two questions
    By jajdoo in forum Newbie
    Replies: 5
    Last Post: 18th July 2010, 22:12
  4. Some questions for Qt?
    By tszzp in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2009, 10:46
  5. A few questions about Qt/Win
    By Bojan in forum Installation and Deployment
    Replies: 3
    Last Post: 16th January 2006, 10:54

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.