Skip to content

Instantly share code, notes, and snippets.

@phantomfive
Created March 23, 2016 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phantomfive/27fc033c8233cc57ffe8 to your computer and use it in GitHub Desktop.
Save phantomfive/27fc033c8233cc57ffe8 to your computer and use it in GitHub Desktop.
Timing loop direction and speed
#include <stdio.h>
#include <sys/resource.h>
int getTime() {
struct rusage r;
if(getrusage(RUSAGE_SELF, &r)!=0) {perror("FAIL RUSAGE");}
double u = (r.ru_utime.tv_sec * 1000) + (r.ru_utime.tv_usec/1000.0);
double s = (r.ru_stime.tv_sec * 1000) + (r.ru_stime.tv_usec/1000.0);
printf("-----------> u %lf, s %lf\n", u, s);
return u + s;
}
int main(){
int j=0;
int i=0;
int k=0;
double t1 = getTime();
for(k=0; k<10;k++) {
for(i=1000000000;i>=0;i--) {
j = i;
}
}
double t2 = getTime();
printf ("t1 %lf, t2 %lf, diff %lf\n", t1, t2, t2-t1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment