diff --git a/cbench.c b/cbench.c index a35e253..3efe746 100644 --- a/cbench.c +++ b/cbench.c @@ -18,17 +18,10 @@ static void as_timespec(uint64_t ns, struct timespec* ts) { ts->tv_nsec = ns % (uint64_t)1000000000L; } -uint64_t run_individual_benchmark(char** argv) +uint64_t run_individual_benchmark(char** argv, int fd) { struct timespec start, end; - int fd = open("/dev/null", O_WRONLY); - if(fd < 0) - { - perror("open"); - return (uint64_t)-1; - } - if(clock_gettime(CLOCK_MONOTONIC, &start) != 0) { perror("clock_gettime"); @@ -50,7 +43,6 @@ uint64_t run_individual_benchmark(char** argv) exit(1); } - close(fd); int status; pid_t result = waitpid(child, &status, 0); @@ -112,11 +104,19 @@ int main(int argc, char** argv) uint64_t min = UINT64_MAX; uint64_t max = 0; + int fd = open("/dev/null", O_WRONLY); + if(fd < 0) + { + perror("open"); + return 1; + } + for(int i = 0; i < iterations; i++) { - uint64_t ns = run_individual_benchmark(argv+1); + uint64_t ns = run_individual_benchmark(argv+1, fd); if(ns == (uint64_t)-1) { + close(fd); return 1; } @@ -124,6 +124,7 @@ int main(int argc, char** argv) if(ns < min) min = ns; if(ns > max) max = ns; } + close(fd); uint64_t average = total / (uint64_t)iterations;