Skip to content

Instantly share code, notes, and snippets.

@frkd-dev
Last active April 13, 2022 08:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frkd-dev/54c304bc010307562ec3434dbd5e76ae to your computer and use it in GitHub Desktop.
Save frkd-dev/54c304bc010307562ec3434dbd5e76ae to your computer and use it in GitHub Desktop.
Measure performance for different time functions
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
/*
Build:
gcc -o time_perf time_perf.c
Run:
./time_perf 10000000
Reference results (Intel Pentium N3700):
time() in 0.182687 sec
clock() in 3.756838 sec
timespec_get() in 0.717233 sec
gettimeofday() in 0.529567 sec
clock_gettime(REALTIME) in 0.708866 sec
clock_gettime(MONOTONIC) in 0.704702 sec
clock_gettime(REALTIME_COARSE) in 0.458660 sec
clock_gettime(MONOTONIC_COARSE) in 0.479514 sec
clock_gettime(MONOTONIC_RAW) in 1.943516 sec
clock_gettime(PROCESS_CPUTIME) in 3.621124 sec
clock_gettime(THREAD_CPUTIME) in 3.527629 sec
*/
void usage()
{
printf(
"USAGE:\n"
" time_perf <count>\n\n"
"\n"
);
}
int main(int argc, char* argv[])
{
clock_t start;
clock_t c;
struct timespec ts;
struct timeval tv;
time_t t;
if(argc < 2) {
usage();
return 1;
}
int count = atoi(argv[1]);
start = clock();
for(int i = 0; i < count; i++)
t = time(NULL);
printf("time() in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
c = clock();
printf("clock() in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
c = timespec_get(&ts, TIME_UTC);
printf("timespec_get() in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
gettimeofday(&tv, NULL);
printf("gettimeofday() in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_REALTIME, &ts);
printf("clock_gettime(REALTIME) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_MONOTONIC, &ts);
printf("clock_gettime(MONOTONIC) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_REALTIME_COARSE, &ts);
printf("clock_gettime(REALTIME_COARSE) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
printf("clock_gettime(MONOTONIC_COARSE) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
printf("clock_gettime(MONOTONIC_RAW) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
printf("clock_gettime(PROCESS_CPUTIME) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i = 0; i < count; i++)
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
printf("clock_gettime(THREAD_CPUTIME) in %f sec\n", (double)(clock() - start)/CLOCKS_PER_SEC);
return 0;
}
@luceinaltis
Copy link

It is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment