1 /* Skeleton for benchmark programs.
2 Copyright (C) 2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #define TIMESPEC_AFTER(a, b) \
26 (((a).tv_sec == (b).tv_sec) ? \
27 ((a).tv_nsec > (b).tv_nsec) : \
28 ((a).tv_sec > (b).tv_sec))
30 main (int argc
, char **argv
)
33 struct timespec start
, end
, runtime
;
35 memset (&runtime
, 0, sizeof (runtime
));
36 memset (&start
, 0, sizeof (start
));
37 memset (&end
, 0, sizeof (end
));
39 clock_getres (CLOCK_PROCESS_CPUTIME_ID
, &start
);
41 /* Measure 1000 times the resolution of the clock. So for a 1ns resolution
42 clock, we measure 1000 iterations of the function call at a time.
43 Measurements close to the minimum clock resolution won't make much sense,
44 but it's better than having nothing at all. */
45 unsigned long iters
= 1000 * start
.tv_nsec
;
47 for (int v
= 0; v
< NUM_VARIANTS
; v
++)
49 /* Run for approximately DURATION seconds. */
50 clock_gettime (CLOCK_MONOTONIC_RAW
, &runtime
);
51 runtime
.tv_sec
+= DURATION
;
54 uint64_t total
= 0, max
= 0, min
= 0x7fffffffffffffff;
57 for (i
= 0; i
< NUM_SAMPLES (v
); i
++)
59 clock_gettime (CLOCK_PROCESS_CPUTIME_ID
, &start
);
60 for (k
= 0; k
< iters
; k
++)
62 clock_gettime (CLOCK_PROCESS_CPUTIME_ID
, &end
);
64 uint64_t cur
= (end
.tv_nsec
- start
.tv_nsec
65 + ((end
.tv_sec
- start
.tv_sec
)
66 * (uint64_t) 1000000000));
78 struct timespec curtime
;
80 memset (&curtime
, 0, sizeof (curtime
));
81 clock_gettime (CLOCK_MONOTONIC_RAW
, &curtime
);
82 if (TIMESPEC_AFTER (curtime
, runtime
))
90 d_total_s
= total
* 1e-9;
93 printf ("%s: ITERS:%g: TOTAL:%gs, MAX:%gns, MIN:%gns, %g iter/s\n",
95 d_total_i
, d_total_s
, max
/ d_iters
, min
/ d_iters
,
96 d_total_i
/ d_total_s
);