FORM v5.0.0-35-g6318119
mytime.cc
1/* #[ License : */
2/*
3 * Copyright (C) 1984-2026 J.A.M. Vermaseren
4 * When using this file you are requested to refer to the publication
5 * J.A.M.Vermaseren "New features of FORM" math-ph/0010025
6 * This is considered a matter of courtesy as the development was paid
7 * for by FOM the Dutch physics granting agency and we would like to
8 * be able to track its scientific use to convince FOM of its value
9 * for the community.
10 *
11 * This file is part of FORM.
12 *
13 * FORM is free software: you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free Software
15 * Foundation, either version 3 of the License, or (at your option) any later
16 * version.
17 *
18 * FORM is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with FORM. If not, see <http://www.gnu.org/licenses/>.
25 */
26/* #] License : */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32// A timing routine for debugging. Only on Unix (where sys/time.h is available).
33#ifdef UNIX
34
35#include <sys/time.h>
36#include <cstdlib>
37#include <cstdio>
38#include <string>
39
40#ifndef timersub
41/* timersub is not in POSIX, but presents on most BSD derivatives.
42 This implementation is borrowed from glibc. (TU 23 Oct 2011) */
43#define timersub(a, b, result) \
44 do { \
45 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
46 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
47 if ((result)->tv_usec < 0) { \
48 --(result)->tv_sec; \
49 (result)->tv_usec += 1000000; \
50 } \
51 } while (0)
52#endif
53
54bool starttime_set = false;
55timeval starttime;
56
57double thetime () {
58 if (!starttime_set) {
59 gettimeofday(&starttime,NULL);
60 starttime_set=true;
61 }
62
63 timeval now,diff;
64 gettimeofday(&now,NULL);
65 timersub(&now,&starttime,&diff);
66 return diff.tv_sec+diff.tv_usec/1000000.0;
67}
68
69std::string thetime_str() {
70 char res[10];
71 snprintf (res,10,"%.4lf", thetime());
72 return res;
73}
74
75#endif // UNIX