YnkDK
YnkDK

Reputation: 741

Repeated timing of a void function in C++

I am trying to time a void function

for (size_t round = 0; round < 5; round++) {
    cpu_time_start = get_cpu_time();
    wall_time_start = get_wall_time();

    scan.assign_clusters(epsilon, mu);

    cpu_time_end = get_cpu_time();
    wall_time_end = get_wall_time();
    ...
}

The first timing yields 300 seconds, while the next four timings yields 0.000002 seconds. This indicates that the void function call to assign_clusters is optimized out. How can I force my program to execute this time consuming function call every time, and yet still use optimization for the rest of the code?

What I usually do is to save the result of the function in question and then print it, but since this is a void function, do I have the same option?

I use the following optimization flags: -std=c++0x -march=native -O2

Upvotes: 4

Views: 261

Answers (1)

mksteve
mksteve

Reputation: 13073

It depends on what is taking the time, to make the fix.

This could be caused by :-

  1. Loading services. Your clustering may be database based, and requires the database services to start (the first time)
  2. Disk caching. The OS will remember data it has read, and be able to provide the data as if it was in memory.
  3. Memory caching. The CPU has different speeds of memory available to it, using the same memory twice, would be faster the second time.
  4. State caching. The data may be in a more amenable state for subsequent runs. This can be thought of as sorting an array twice. The second time is already sorted, which can produce a speed up.

Service starting can be a number of seconds.

Disk cache approx 20x speed up. Memory cache approx 6x speed up State caching, can be unbounded.

I think your code needs to reset the scan object, to ensure it does the work again

Upvotes: 2

Related Questions