Thread: How to get program execution time

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    How to get program execution time

    Hi, I'm trying to write more efficient code, but it's kind of important I have a way of measuring how fast execution is depending on which code I use. I'm just wondering what a good and precise way is of doing this. Do most people just do basic time() math? Thanks.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    clock() is more precise than time, and at least some systems actually return the CPU time used by the current process, rather than the "wall-clock-time". Although sometimes BOTH wall-clock time and CPU usage is useful. In cases where your code is running 99% of the time, it doesn't really make much difference.

    You need to use the macro CLOCKS_PER_SEC to translate your clock() result into seconds.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    I can't seem to get it to work properly, what am I doing wrong? This is my code, it prints '-61' every single execution.

    Code:
     clock_t ticks = clock() / CLOCKS_PER_SEC;
    
       for (int i = 0; i < 4; i++)
          for (int y = 0; y < 13; y++) {
             deck[(y + 1) + i * 13] = i;
             deck[(y + 1) + i * 13] <<= 4;
             deck[(y + 1) + i * 13] |= y + 1;
       }
    
       clock_t ticks2 = clock() / CLOCKS_PER_SEC;
       cout << ticks2 - ticks << endl;
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This code does so little work it is practically immeasurable anyway. You may need to repeat this operation thousand or millions of times to get some good time.

    You may also want to turn the difference to seconds, not the start and end time.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That loop will take, probably, around 200-500 clock-cycles compiled with a decent compiler, which is much less than 1 microsecond (2000 clock-cycles at 2GHz -> 1 microsecond).

    You could possibly make it faster by doing the whole assignment in one step:
    Code:
             deck[(y + 1) + i * 13] = (i << 4) | (y + 1);
    Although it may not be any faster if the compiler does it's optimizing right, it is an obvious improvement on the posted code.

    Finally, why do you need to store bit patterns - storing the "colour" and the "value" as two different fields in a struct would make the code much easier to write, easier to read, and most likely also faster. I don't think "wasting" some 52 bytes or so will make any noticable difference to the performance.

    I wouldn't bother measuring that. If you really MUST measure it, use
    http://en.wikipedia.org/wiki/Time_Stamp_Counter
    But bear in mind that this method does have it's problems too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Ok cheers, you say storing it in structs would be faster? How is that possible? I assumed working with single bytes would be way faster than working with structs etc.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pobri19 View Post
    Ok cheers, you say storing it in structs would be faster? How is that possible? I assumed working with single bytes would be way faster than working with structs etc.
    Well, storing a byte (char) is (most likely) faster than shifting and oring two values together and then storing that in one element. Extracting the data out of such a combined value is definitely slower than just grabbing a byte out of memory. One reason that either of these is likely to be faster is that they are independent of each other, so the processor can do more in parallel.

    Not to mention that the code is much simpler to deal with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Ok, well thanks for your help matsp! =)
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    We should mention here that you are writing in C++, so make sure that you HIDE how you store your actual data, so that you can later on change your implementation of how to store each card [I'm assuming from the numbers involved that it is regular playing cards].

    Also "beware of premature optimization".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. How to restrict a program in time consumed
    By gogoc in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2008, 11:10 AM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  5. write a program that will set the time to 5:58
    By jupimako in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2003, 03:51 AM