Results 1 to 4 of 4

Thread: Qt true,false speed

  1. #1
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt true,false speed

    Hello,

    Is there any difference in speed in writing code like Code1 and Code2 ?
    I heard that Code2 is faster than Code1... ?

    Code1:
    Qt Code:
    1. if (strData.isEmpty() == true)
    2. {
    3. ...
    To copy to clipboard, switch view to plain text mode 

    Code2:
    Qt Code:
    1. if (strData.isEmpty())
    2. {
    3. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt true,false speed

    You would have to check a disassembly to be absolutely sure, but the comparison has to be made in either case, and most compilers will produce equivalent code for both.

    Any difference would be so small it would be difficult to measure. The effect on runtime would be negligible.

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

    Default Re: Qt true,false speed

    Well, the second code is certainly faster than the first to type, maybe thats what they were talking about?

    As for speed, well, you have two different comparisons, so it depends on how its compiled. The first checks against true only, whilst the second checks for any non-false value, so the second might be one assembly instruction less on some architectures (as they can just check a flag rather than comparing with a fixed value), but as already said, the difference would be incredibly small.

    I'd go with the second simply because it's quicker to type

  4. #4
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt true,false speed

    I'd probably go with the second as well, but only because the function call (isEmpty()) makes it very clear what is being tested. In other cases, the function name alone is often not so clear, and adding the explicit test condition removes all doubt as to what the expected outcome is. From a maintenance standpoint, clarity often trumps small differences in performance.

Similar Threads

  1. Replies: 9
    Last Post: 22nd July 2010, 08:40
  2. QTabWidget and .setTabsClosable(True)
    By prof.ebral in forum Newbie
    Replies: 3
    Last Post: 26th February 2010, 04:36
  3. Qt for S60, compiler warning regarding TRUE/FALSE
    By Archimedes in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 22nd October 2009, 14:09
  4. why Qt's forever macro is for(;;) rather than while(true).
    By babu198649 in forum General Programming
    Replies: 1
    Last Post: 13th June 2009, 14:21
  5. Is this true, Qt lgpl license..
    By tgreaves in forum General Discussion
    Replies: 5
    Last Post: 8th March 2009, 11:34

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.