Precise time measurement in C

I'm trying to figure out a way to write code that could pinpoint the time it takes to perform a search in BST. Currently, he uses time and the total number of elements of the order of 10 ^ 5. It looks something like this: -

clock_t begin, end; begin = clock(); ... ... ... end = clock(); difference = (end-begin)/CLOCKS_PER_SECOND; 

However, this does not give me the accuracy I am looking for. Are there any other libc functions that I could use?

+8
c linux
source share
6 answers

To test your algorithm, you need to do a few repetitions to get in the range of at least hundreds of milliseconds. (This is standard practice). To compare an algorithm that runs only in user space (without threads, system calls, etc.), you should use getrusage(RUSAGE_SELF, &r) and use the r.ru_utime value, which contains seconds and microseconds.

+2
source share

If your library supports it, C11 has timespec_get() , which will measure up to nanoseconds, depending on the resolution of your system.

+2
source share

If you use a benchmark with an Intel processor. Perhaps you can try the RDTSC and RDTSCP instructions.

here is the instruction document

+1
source share

Bst? What precision do you want? Splitting on CLOCKS_PER_SECOND 10 ^ 6 on a 32-bit system should give 6-digit accuracy?

Do you make the result double?

try

 difference = (double)(end-begin)/CLOCKS_PER_SECOND; 

Please note that the difference must contain double.

0
source share

Qt has a QElapsedTimer that supports measurement up to nanoseconds. I can’t testify to how accurate this is; IIRC uses various implementations on different platforms. Unfortunately, this is C ++, which may not suit you. Also:

On platforms that do not provide nanosecond resolution, a Return value would be a better estimate.

The clock() function is suitable for rough measurements, but works in milliseconds. Contrary to its name, I do not think that it measures the processor clock, since the modern processor clock speeds can vary quite strongly, which makes it impossible to accurately determine the actual time solely on the basis of the CPU clock pulses. IMO, this concept dates back to the times when the processor frequencies were constant, there was no power management, automatic acceleration without turbocharging, and in general.

EDIT: Also found this (time.h):

  int clock_gettime(clockid_t clk_id, struct timespec *tp); ... and the target struct... struct timespec { time_t tv_sec; /* seconds */ long tv_nsec; /* nanoseconds */ }; ... and the clock options... CLOCK_REALTIME System-wide realtime clock. Setting this clock requires appropriate privileges. CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some unspecified starting point. CLOCK_PROCESS_CPUTIME_ID High-resolution per-process timer from the CPU. CLOCK_THREAD_CPUTIME_ID Thread-specific CPU-time clock. 
0
source share

What you do is very similar to what I did recently.

I really think the int gettimeofday(struct timeval *tv, struct timezone *tz); function int gettimeofday(struct timeval *tv, struct timezone *tz); suits you. Time information will be placed in struct timeval tv , which receives the time in seconds and microseconds. struct timeval on the struct timeval page:

 struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; 

A quick example of measuring time using gettimeofday :

 struct timeval time; if(gettimeofday( &time, 0 )) return -1; long cur_time = 1000000 * time.tv_sec + time.tv_usec; double sec = cur_time / 1000000.0; 

The longer example is simplified and easy to port as a C ++ class for convenient use. The code was placed on my github: https://github.com/lulyon/LinuxTimeCounter , which is used in the real world.

0
source share

All Articles