-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.c
78 lines (63 loc) · 1.48 KB
/
benchmark.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (C) 2014 Mitchell Perilstein
* Licensed under GNU LGPL Version 3. See LICENSING file for details.
*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
void usage()
{
fprintf(stderr, "Usage: benchmark TRIALS ITERATIONS\n");
exit(1);
}
struct tm * makeit(long ndates)
{
struct tm * date_array = (struct tm *)malloc(ndates * sizeof(struct tm));
if (!date_array)
{
fprintf(stderr, "Too much malloc\n");
exit(1);
}
long s;
for (s=0; s<ndates; s++)
{
time_t t = s + 1; // start at year 1
gmtime_r(&t, &date_array[s]);
}
return date_array;
}
float timeit(struct tm *dates, long ndates)
{
struct timeval tbeg, tend, tres;
gettimeofday(&tbeg, NULL);
long s;
for (s=0; s<ndates; s++)
{
time_t made = mktime(&dates[s]);
}
gettimeofday(&tend, NULL);
return ((tend.tv_sec * 1e6 + tend.tv_usec)
- (tbeg.tv_sec * 1e6 + tbeg.tv_usec)) / 1e6;
}
int main(int argc, char **argv)
{
if (argc < 3)
usage();
long trials = atol(argv[1]);
long ndates = atol(argv[2]);
if (ndates<1 || trials<1)
usage();
struct tm *dates = makeit(ndates);
float total = 0.0;
long i;
for (i=0; i<trials; i++)
{
float secs = timeit(dates, ndates);
printf("%3d: %f s\n", (int)i, secs);
total += secs;
}
printf("avg: %f s\n", total/trials);
exit(0);
}