# How to get program execution time

• 01-20-2009
pobri19
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.
• 01-20-2009
matsp
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
• 01-20-2009
pobri19
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;```
• 01-20-2009
anon
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.
• 01-20-2009
matsp
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
• 01-20-2009
pobri19
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.
• 01-20-2009
matsp
Quote:

Originally Posted by pobri19
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
• 01-20-2009
pobri19
Ok, well thanks for your help matsp! =)
• 01-20-2009
matsp
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