40 lines · c
1#include "test.h"2#include <time.h>3 4int bench_nthread;5int bench_niter;6int bench_mode;7 8void bench(); // defined by user9void start_thread_group(int nth, void(*f)(int tid));10 11int main(int argc, char **argv) {12 bench_nthread = 2;13 if (argc > 1)14 bench_nthread = atoi(argv[1]);15 bench_niter = 100;16 if (argc > 2)17 bench_niter = atoi(argv[2]);18 if (argc > 3)19 bench_mode = atoi(argv[3]);20 21 timespec tp0;22 clock_gettime(CLOCK_MONOTONIC, &tp0);23 bench();24 timespec tp1;25 clock_gettime(CLOCK_MONOTONIC, &tp1);26 unsigned long long t =27 (tp1.tv_sec * 1000000000ULL + tp1.tv_nsec) -28 (tp0.tv_sec * 1000000000ULL + tp0.tv_nsec);29 fprintf(stderr, "%llu ns/iter\n", t / bench_niter);30 fprintf(stderr, "DONE\n");31}32 33void start_thread_group(int nth, void(*f)(int tid)) {34 pthread_t *th = (pthread_t*)malloc(nth * sizeof(pthread_t));35 for (int i = 0; i < nth; i++)36 pthread_create(&th[i], 0, (void*(*)(void*))f, (void*)(long)i);37 for (int i = 0; i < nth; i++)38 pthread_join(th[i], 0);39}40