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/>. */
26 main (int argc
, char **argv
)
28 unsigned long i
, j
, k
;
29 uint64_t total
= 0, max
= 0, min
= 0x7fffffffffffffff;
30 struct timespec start
, end
;
32 memset (&start
, 0, sizeof (start
));
33 memset (&end
, 0, sizeof (end
));
35 clock_getres (CLOCK_PROCESS_CPUTIME_ID
, &start
);
37 /* Measure 1000 times the resolution of the clock. So for a 1ns resolution
38 clock, we measure 1000 iterations of the function call at a time.
39 Measurements close to the minimum clock resolution won't make much sense,
40 but it's better than having nothing at all. */
41 unsigned long iters
= 1000 * start
.tv_nsec
;
42 unsigned long total_iters
= ITER
/ iters
;
44 for (i
= 0; i
< NUM_SAMPLES
; i
++)
46 for (j
= 0; j
< total_iters
; j
++)
48 clock_gettime (CLOCK_PROCESS_CPUTIME_ID
, &start
);
49 for (k
= 0; k
< iters
; k
++)
51 clock_gettime (CLOCK_PROCESS_CPUTIME_ID
, &end
);
53 uint64_t cur
= (end
.tv_nsec
- start
.tv_nsec
54 + ((end
.tv_sec
- start
.tv_sec
)
55 * (uint64_t) 1000000000));
67 double d_total_s
= total
* 1e-9;
68 double d_iters
= iters
;
69 double d_total_i
= (double)ITER
* NUM_SAMPLES
;
70 printf (FUNCNAME
": ITERS:%g: TOTAL:%gs, MAX:%gns, MIN:%gns, %g iter/s\n",
71 d_total_i
, d_total_s
, max
/ d_iters
, min
/ d_iters
,
72 d_total_i
/ d_total_s
);