Results 1 to 5 of 5

Thread: Simple stopwatch to time some code?

  1. #1
    Join Date
    Feb 2013
    Posts
    65
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Simple stopwatch to time some code?

    What's the simplest way to measure the amount of time a piece of code takes using QTimer? I just want to time the code and convert it to text to output to a message box. Thanks in advance for any help.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Simple stopwatch to time some code?

    QTime::start() or QTime::restart() followed by QTime::elapsed()

  3. #3
    Join Date
    Feb 2013
    Posts
    65
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Simple stopwatch to time some code?

    Thanks. That worked.


    Added after 58 minutes:


    This is what I used:

    Header file for class

    myclass.h

    Qt Code:
    1. #include <QMessageBox>
    2. #include <QTime>
    3.  
    4. double time;
    5. QMessageBox msgBox;
    To copy to clipboard, switch view to plain text mode 

    In myclass.cpp file for class

    Qt Code:
    1. void myclass::Method1(void)
    2. {
    3.  
    4. // Message Box here to introduce method
    5.  
    6. time = 0.0
    7. t.start();
    8.  
    9. // Code to time here
    10.  
    11. time += ((double) t.elapsed())/1000.0;
    12.  
    13.  
    14. }
    15.  
    16. void myclass::Method2(void)
    17. {
    18.  
    19. // Message Box here to introduce method
    20.  
    21. t.restart();
    22.  
    23. // Code to time here
    24.  
    25. time += ((double) t.elapsed())/1000.0;
    26.  
    27. }
    28.  
    29. void myclass::Method3(void)
    30. {
    31.  
    32. // Message Box here to introduce method
    33.  
    34. t.restart();
    35.  
    36. // Code to time here
    37.  
    38. time += ((double) t.elapsed())/1000.0;
    39.  
    40. }
    41.  
    42. void myclass::Method4(void)
    43. {
    44.  
    45. QString Temp;
    46.  
    47. // Message Box here to introduce method
    48.  
    49. t.restart();
    50.  
    51. // Code to time here
    52.  
    53. time += ((double) t.elapsed())/1000.0;
    54.  
    55. Temp = "Seconds = " + QString::number(time);
    56.  
    57. msgBox.setText(Temp);
    58. msgBox.exec();
    59.  
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by te777; 23rd June 2015 at 02:52.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple stopwatch to time some code?

    Use QElapsedTimer. It is better for calculate elapsed time.

  5. #5
    Join Date
    Feb 2013
    Posts
    65
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Simple stopwatch to time some code?

    Thanks. I'll look into it.


    Added after 43 minutes:


    I tried QElapsedTimer. It seems to have some overhead associated with use of the class. But very minor. The difference is not that much. But QTime seemed to give me a little bit more quicker execution time. I measured the number crunching code of 4 methods (3 times with QTime and 3 with QElapsedTimer) and ended up with the following results:

    QElapsedTimer

    8.06 s
    8.028 s
    8.05 s

    Average = 8.046 s

    QTime

    8.013 s
    8.021 s
    8.013 s

    Average = 8.016 s

    It seems the use of QTime ended up with a little bit more quicker execution time. It would probably be more significant for more time consuming processes. The StackOverflow thread following seems to cover the use of QElapsedTime pretty well: http://stackoverflow.com/questions/2...sed-time-in-qt
    Last edited by te777; 23rd June 2015 at 14:06.

Similar Threads

  1. Replies: 1
    Last Post: 1st May 2015, 11:09
  2. A Stopwatch in qt GUI ?
    By 020394 in forum Qt Programming
    Replies: 12
    Last Post: 22nd January 2015, 15:52
  3. Create Stopwatch With QTimer
    By ramin.lich in forum Newbie
    Replies: 2
    Last Post: 29th August 2014, 16:39
  4. How to code a (simple ?) cUrl command in Qt ?
    By nils_heidorn in forum Newbie
    Replies: 15
    Last Post: 30th August 2013, 12:36
  5. Simple code causes segmentation fault...in debug
    By Raccoon29 in forum Qt Programming
    Replies: 18
    Last Post: 22nd December 2009, 10:15

Tags for this Thread

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.