Line data Source code
1 : /**
2 : * @file gensvm_timer.c
3 : * @author G.J.J. van den Burg
4 : * @date 2014-01-07
5 : * @brief Utility functions relating to time
6 : *
7 : * @details
8 : * This file contains a simple function for calculating the time in seconds
9 : * elapsed between two Timer() calls.
10 : *
11 : * @copyright
12 : Copyright 2016, G.J.J. van den Burg.
13 :
14 : This file is part of GenSVM.
15 :
16 : GenSVM is free software: you can redistribute it and/or modify
17 : it under the terms of the GNU General Public License as published by
18 : the Free Software Foundation, either version 3 of the License, or
19 : (at your option) any later version.
20 :
21 : GenSVM is distributed in the hope that it will be useful,
22 : but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 : GNU General Public License for more details.
25 :
26 : You should have received a copy of the GNU General Public License
27 : along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
28 :
29 : */
30 :
31 : #include "gensvm_timer.h"
32 :
33 : /**
34 : * @brief Calculate the time between two time recordings
35 : *
36 : * @details
37 : * This function should be used with time recordings done by clock_gettime()
38 : * using CLOCK_MONOTONIC_RAW. For this, the Timer() macro has been defined in
39 : * the header file. Example usage:
40 : *
41 : * @code
42 : * struct timespec start, stop;
43 : * Timer(start);
44 : * // do some work //
45 : * Timer(stop);
46 : * double duration = gensvm_elapsed_time(&start, &stop);
47 : * @endcode
48 : *
49 : * This approach to measuring time has been chosen since CLOCK_MONOTONIC_RAW
50 : * is guaranteed to be monotonically increasing, and is not affected by leap
51 : * seconds or other changes to the system clock. It is therefore thread-safe
52 : * and can be used to accurately measure time intervals.
53 : *
54 : * @param[in] start starting time
55 : * @param[in] stop end time
56 : * @returns time elapsed in seconds
57 : */
58 1 : double gensvm_elapsed_time(struct timespec *start, struct timespec *stop)
59 : {
60 1 : double delta_s = stop->tv_sec - start->tv_sec;
61 1 : double delta_ns = stop->tv_nsec - start->tv_nsec;
62 1 : return delta_s + delta_ns * 1e-9;
63 : }
|