Reputation: 2023
timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
---code--lookup in a unordered map <string, int>
clock_gettime(CLOCK_MONOTONIC, &end);
int diff = diff_ns(end, start);
Results:
SAMPLE MIN(ns) MAX(ns) AVG(ns)
100 1000 3000 1430
500 1000 2000 1436
1000 0 16000 1441
5000 0 15000 1479
10000 0 26000 1489
50000 0 363000 1589
100000 0 110000 1591
200000 0 804000 1659
300000 0 118000 1668
400000 1000 354000 1701
500000 0 8679000 1712
600000 0 809000 1701
700000 0 373000 1704
800000 0 850000 1716
900000 0 856000 1736
1000000 0 817000 1730
How to ignore time spent by CPU to calculate the clock_gettime as in the end we take time spent also by clock_gettime call?
I am running tests in single threaded program...but how to make sure there is no context switch happening, because other processes may also be running on VM
Sometimes I get zero time taken, since I am measuring in nanoseconds, I find it strange, how something can execute in zero nanoseconds?
Upvotes: 2
Views: 361
Reputation: 64203
I am running tests in single threaded program...but how to make sure there is no context switch happening, because other processes may also be running on VM
Kill all processes that are not needed, and if you have an option, raise the priority of the profiled process.
Other then that, you can use prof or callgrind to profile your program.
Sometimes I get zero time taken, since I am measuring in nanoseconds, I find it strange, how something can execute in zero nanoseconds?
You are getting 0 ns execution time, because the CPU clock precision is above 10 ms.
Measure the time after more iterations, and you'll get better results.
Do more lookups, and then average the value :
timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
for (int i=0;i<10000;++i)
---code--lookup in a unordered map <string, int>
clock_gettime(CLOCK_MONOTONIC, &end);
int diff = diff_ns(end, start)/10000;
Like this time spent in clock_gettime will be neglected.
Upvotes: 4