fix: Open /dev/null only once and close it at the end

Before this patch, we would open and close /dev/null every
iteration.
This commit is contained in:
apio 2023-05-03 17:05:17 +02:00
parent 881b748209
commit 1810a0a89b
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -18,17 +18,10 @@ static void as_timespec(uint64_t ns, struct timespec* ts) {
ts->tv_nsec = ns % (uint64_t)1000000000L; 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; 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) if(clock_gettime(CLOCK_MONOTONIC, &start) != 0)
{ {
perror("clock_gettime"); perror("clock_gettime");
@ -50,7 +43,6 @@ uint64_t run_individual_benchmark(char** argv)
exit(1); exit(1);
} }
close(fd);
int status; int status;
pid_t result = waitpid(child, &status, 0); pid_t result = waitpid(child, &status, 0);
@ -112,11 +104,19 @@ int main(int argc, char** argv)
uint64_t min = UINT64_MAX; uint64_t min = UINT64_MAX;
uint64_t max = 0; 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++) 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) if(ns == (uint64_t)-1)
{ {
close(fd);
return 1; return 1;
} }
@ -124,6 +124,7 @@ int main(int argc, char** argv)
if(ns < min) min = ns; if(ns < min) min = ns;
if(ns > max) max = ns; if(ns > max) max = ns;
} }
close(fd);
uint64_t average = total / (uint64_t)iterations; uint64_t average = total / (uint64_t)iterations;