Skip to content

Instantly share code, notes, and snippets.

@chadbrewbaker
Created July 25, 2011 01:50
Show Gist options
  • Save chadbrewbaker/1103393 to your computer and use it in GitHub Desktop.
Save chadbrewbaker/1103393 to your computer and use it in GitHub Desktop.
Timer resolution and average behavior test
/*
timetest.cpp
A simple program to demonstrate the difference in timers on your platform.
Tue Dec 21 15:11:06 CST 2010
Chad Brewbaker
crb002@gmail.com
*/
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#ifdef __APPLE__
#include <stdint.h>
#include <mach/mach_time.h>
#endif
int RUNS = 1000000;
double clock_res(unsigned* while_ticks){
clock_t t1, t2;
unsigned ticks =1;
t1 = t2 = clock();
// loop until t2 gets a different value
while(t1 == t2)
{t2 = clock(); ticks++;}
*while_ticks = ticks;
return (double)(t2 - t1) / CLOCKS_PER_SEC * 1000;
}
double gettimeofday_res(unsigned* while_ticks){
timeval t1, t2;
double elapsedTime;
unsigned ticks = 1;
gettimeofday(&t1, NULL);
t2=t1;
while( (t1.tv_usec == t2.tv_usec) && (t1.tv_sec == t2.tv_sec) ){
gettimeofday(&t2, NULL);
ticks++;
}
// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
*while_ticks = ticks;
return elapsedTime;
}
#ifdef __APPLE__
double mach_res(unsigned* while_ticks){
uint64_t t1, t2;
unsigned ticks = 1;
mach_timebase_info_data_t info;
mach_timebase_info(&info);
t1 = t2 = mach_absolute_time();
/* Do some code */
while( t1 == t2 )
{t2 = mach_absolute_time(); ticks++;}
uint64_t duration = t2 - t1;
/* Convert to nanoseconds */
duration *= info.numer;
duration /= info.denom;
*while_ticks = ticks;
return (double) duration * 0.000001 ;
}
#endif
int main()
{
int i;
double total;
double max;
double res;
unsigned ticks, min_ticks, max_ticks, tick_total;
printf("Function_name \tave(ms) \tmax(ms) \tmin_ticks \tmax_ticks \tave_ticks\n");
max_ticks = ticks = tick_total = 0;
min_ticks = 1000000;
total = max = 0.0;
for(i=0; i<RUNS;i++){
res = clock_res(&ticks);
if(max < res)
max = res;
if(ticks > max_ticks)
max_ticks = ticks;
if(min_ticks > ticks)
min_ticks = ticks;
tick_total += ticks;
total += res;
}
printf("clock() \t%.10f \t%.10f \t%d \t%d \t%d\n", total/(double)RUNS, max, min_ticks, max_ticks, tick_total/RUNS);
max_ticks = ticks = tick_total = 0;
min_ticks = 1000000;
total = max = 0.0;
for(i=0; i<RUNS;i++){
res = gettimeofday_res(&ticks);
if(max < res)
max = res;
if(ticks > max_ticks)
max_ticks = ticks;
if(min_ticks > ticks)
min_ticks = ticks;
tick_total += ticks;
total += res;
}
printf("gettimeofday() \t%.10f \t%.10f \t%d \t%d \t%d\n", total/(double)RUNS, max, min_ticks, max_ticks, tick_total/RUNS);
#ifdef __APPLE__
max_ticks = ticks = tick_total = 0;
min_ticks = 1000000;
total = max = 0.0;
for(i=0; i<RUNS;i++){
res = mach_res(&ticks);
if(max < res)
max = res;
if(ticks > max_ticks)
max_ticks = ticks;
if(min_ticks > ticks)
min_ticks = ticks;
tick_total += ticks;
total += res;
}
printf("mach_absolute_time() \t%.10f \t%.10f \t%d \t%d \t%d\n", total/(double)RUNS, max, min_ticks, max_ticks, tick_total/RUNS);
#endif
return 0;
}
@chadbrewbaker
Copy link
Author

Output for a Core2Duo MacBook.

Function_name ave(ms) max(ms) min_ticks max_ticks ave_ticks
clock() 0.0013114960 0.0130000000 2 3 2
gettimeofday() 0.0010004230 0.1180000000 2 35 13
mach_absolute_time() 0.0000366985 0.0015940000 2 2 2

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