Created
August 4, 2016 16:05
-
-
Save postwait/43d067d76de8b7801c1b3b48ee9f2118 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/time.h> | |
#include <pthread.h> | |
#include <stdlib.h> | |
void log_results(const char *name, int nt, double nops, double tdiff) { | |
printf(" === %s (%d threads, %0.0f total ops) ===\n", name, nt, nops); | |
printf("\tTotal time: %0.2f ms\n", tdiff*1000); | |
printf("\tTime per op: %0.1f ns\n", 1000000000.0 * (tdiff/nops)); | |
printf("\tOps per sec: %0.0f ops\n", nops/tdiff); | |
printf("\n"); | |
} | |
struct work { | |
struct timeval start, finish; | |
long nops_in; | |
long nops_out; | |
double tdiff; | |
}; | |
static inline void start_work(struct work *w) { | |
gettimeofday(&w->start, NULL); | |
} | |
static inline void finish_work(struct work *w, long ops) { | |
w->nops_out = ops; | |
gettimeofday(&w->finish, NULL); | |
w->tdiff = (double)(w->finish.tv_sec - w->start.tv_sec); | |
w->tdiff += ((double)w->finish.tv_usec - (double)w->start.tv_usec)/1000000.0; | |
} | |
void runtrial(const char *name, void *(*f)(void *), int nt, int nl) { | |
int i; | |
void *unused; | |
pthread_t tid[nt]; | |
struct timeval start, finish; | |
struct work nop[nt]; | |
for(i=0;i<nt;i++) nop[i].nops_in = nl; | |
for(i=0;i<nt;i++) pthread_create(&tid[i], NULL, f, &nop[i]); | |
for(i=0;i<nt;i++) pthread_join(tid[i], &unused); | |
double total_nops = 0.0, tot_time = 0.0; | |
for(i=0;i<nt;i++) { | |
total_nops += nop[i].nops_out; | |
tot_time += nop[i].tdiff; | |
} | |
log_results(name, nt, total_nops, tot_time); | |
} | |
void *gethrtime_test(void *vw) { | |
hrtime_t t; | |
struct work *w = vw; | |
long i; | |
start_work(w); | |
for(i=0;i<w->nops_in;i++) t = gethrtime(); | |
finish_work(w, i); | |
return NULL; | |
} | |
void *gettimeofday_test(void *vw) { | |
struct timeval dummy; | |
struct work *w = vw; | |
long i; | |
start_work(w); | |
for(i=0;i<w->nops_in;i++) gettimeofday(&dummy, NULL); | |
finish_work(w, i); | |
return NULL; | |
} | |
int main() { | |
runtrial("gettimeofday", gettimeofday_test, 1, 10000000); | |
runtrial("gettimeofday", gettimeofday_test, 16, 10000000); | |
runtrial("gethrtime", gethrtime_test, 1, 10000000); | |
runtrial("gethrtime", gethrtime_test, 16, 10000000); | |
return 0; | |
} |
; dis -F gethrtime /usr/lib/libc.so ⏎ jesus2
disassembly for /usr/lib/libc.so
gethrtime()
gethrtime: b8 03 00 00 00 movl $0x3,%eax
gethrtime+0x5: cd d2 int $0xd2
gethrtime+0x7: c3 ret
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
; dis -F gettimeofday /usr/lib/libc.so jesus2
disassembly for /usr/lib/libc.so
_gettimeofday()
_gettimeofday: b8 05 00 00 00 movl $0x5,%eax
_gettimeofday+0x5: cd d2 int $0xd2
_gettimeofday+0x7: 8b 4c 24 04 movl 0x4(%esp),%ecx
_gettimeofday+0xb: e3 0f jcxz +0xf <_gettimeofday+0x1c>
_gettimeofday+0xd: 89 01 movl %eax,(%ecx)
_gettimeofday+0xf: b8 d3 4d 62 10 movl $0x10624dd3,%eax
_gettimeofday+0x14: f7 ea imull %edx
_gettimeofday+0x16: c1 fa 06 sarl $0x6,%edx
_gettimeofday+0x19: 89 51 04 movl %edx,0x4(%ecx)
_gettimeofday+0x1c: 31 c0 xorl %eax,%eax
_gettimeofday+0x1e: c3 ret