Astaroth  2.2
timer_hires.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2020, Johannes Pekkila, Miikka Vaisala.
3 
4  This file is part of Astaroth.
5 
6  Astaroth is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Astaroth is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
31 #pragma once
32 #include <stdio.h> // perror
33 #include <time.h>
34 
35 typedef struct timespec Timer;
36 // Contains at least the following members:
37 // time_t tv_sec;
38 // long tv_nsec;
39 
40 static inline int
41 timer_reset(Timer* t)
42 {
43  const int retval = clock_gettime(CLOCK_REALTIME, t);
44  if (retval == -1)
45  perror("clock_gettime failure");
46 
47  return retval;
48 }
49 
50 static inline long
51 timer_diff_nsec(const Timer start)
52 {
53  Timer end;
54  timer_reset(&end);
55  const long diff = (end.tv_sec - start.tv_sec) * 1000000000l + (end.tv_nsec - start.tv_nsec);
56  return diff;
57 }
58 
59 static inline void
60 timer_diff_print(const Timer t)
61 {
62  printf("Time elapsed: %g ms\n", timer_diff_nsec(t) / 1e6);
63 }
Timer
struct timespec Timer
Definition: timer_hires.h:35