3 Copyright (C) 2013 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
45 #if HAVE_CLOCK_GETTIME && defined CLOCK_PROCESS_CPUTIME_ID
46 #define TRY_CLOCK_GETTIME 1
47 struct timespec cgt_start;
49 static void NORETURN PRINTF_STYLE(1,2)
50 die(const char *format, ...)
53 va_start(args, format);
54 vfprintf(stderr, format, args);
64 return clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now) == 0;
70 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cgt_start) < 0)
71 die("clock_gettime failed: %s\n", strerror(errno));
78 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0)
79 die("clock_gettime failed: %s\n", strerror(errno));
81 return end.tv_sec - cgt_start.tv_sec
82 + 1e-9 * (end.tv_nsec - cgt_start.tv_nsec);
84 #endif /* !HAVE_CLOCK_GETTIME */
86 static clock_t clock_start;
89 clock_time_start(void)
91 clock_start = clock();
97 return (double) (clock() - (clock_start)) / CLOCKS_PER_SEC;
100 void (*time_start)(void) = clock_time_start;
101 double (*time_end)(void) = clock_time_end;
105 /* Choose timing function */
106 #if TRY_CLOCK_GETTIME
109 time_start = cgt_time_start;
110 time_end = cgt_time_end;
114 fprintf(stderr, "clock_gettime not working, falling back to clock\n");
115 time_start = clock_time_start;
116 time_end = clock_time_end;