Skip to content

Instantly share code, notes, and snippets.

@emilsoman
Created November 5, 2014 20:10
Show Gist options
  • Save emilsoman/deafac9978b8785ae3c4 to your computer and use it in GitHub Desktop.
Save emilsoman/deafac9978b8785ae3c4 to your computer and use it in GitHub Desktop.
Compiled with "gcc linux_time.c && ./a.out" , getting -> undefined reference to `clock_gettime'. Fixed using "gcc test_linux.c -Xlinker -lrt"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
double get_wall_time(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + (tv.tv_usec/1000000.0);
}
double get_cpu_time(){
struct timespec clock;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID , &clock);
return clock.tv_sec + (clock.tv_nsec/1000000000.0);
}
int main() {
double wall_time1 = get_wall_time();
double cpu_time1 = get_cpu_time();
int count = 0;
int i=0;
for (; i < (1 << 30); ++i)
{
count += 1;
}
double wall_time2 = get_wall_time();
double cpu_time2 = get_cpu_time();
fprintf(stderr, "Count = %u -- wall = %f -- cpu = %f \n", count, wall_time2 - wall_time1, cpu_time2 - cpu_time1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment