Lattice Microbes 2.5
This is for whole cell modeling
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1/*
2 * University of Illinois Open Source License
3 * Copyright 2016-2018 Luthey-Schulten Group,
4 * All rights reserved.
5 *
6 * Developed by: Luthey-Schulten Group
7 * University of Illinois at Urbana-Champaign
8 * http://www.scs.uiuc.edu/~schulten
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy of
11 * this software and associated documentation files (the Software), to deal with
12 * the Software without restriction, including without limitation the rights to
13 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 * of the Software, and to permit persons to whom the Software is furnished to
15 * do so, subject to the following conditions:
16 *
17 * - Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimers.
19 *
20 * - Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimers in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * - Neither the names of the Luthey-Schulten Group, University of Illinois at
25 * Urbana-Champaign, nor the names of its contributors may be used to endorse or
26 * promote products derived from this Software without specific prior written
27 * permission.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 * OTHER DEALINGS WITH THE SOFTWARE.
36 *
37 * Author(s): Tyler M. Earnest
38 */
39
40#ifndef TIMER_HH_
41#define TIMER_HH_
42
43#ifdef LINUX
44#include <ctime>
45
46#ifdef __MACH__
47#include <mach/clock.h>
48#include <mach/mach.h>
49#endif
50
51class Timer {
52 timespec now,then;
53 void get_time(timespec* t) {
54#ifdef __MACH__
55 clock_serv_t cclock;
56 mach_timespec_t mts;
57 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
58 clock_get_time(cclock, &mts);
59 mach_port_deallocate(mach_task_self(), cclock);
60 t->tv_sec = mts.tv_sec;
61 t->tv_nsec = mts.tv_nsec;
62#else
63 clock_gettime(CLOCK_MONOTONIC, t);
64#endif
65 }
66
67public:
68
69 double
70 tock()
71 {
72 long long int sec,nsec;
73
74 get_time(&now);
75
76 if ((now.tv_nsec-then.tv_nsec)<0)
77 {
78 sec = now.tv_sec-then.tv_sec-1;
79 nsec = 1000000000L+now.tv_nsec-then.tv_nsec;
80 }
81 else
82 {
83 sec = now.tv_sec-then.tv_sec;
84 nsec = now.tv_nsec-then.tv_nsec;
85 }
86 return sec + nsec*1e-9;
87 }
88
89 void tick() { get_time(&then); }
90
91 Timer() { tick(); }
92};
93#endif /* LINUX */
94
95#ifdef MACOSX
96/* https://developer.apple.com/library/mac/qa/qa1398/_index.html */
97#include <mach/mach_time.h>
98
99class Timer {
100 uint64_t now,then;
101 mach_timebase_info_data_t base;
102 void get_time(uint64_t* t) { *t=mach_absolute_time(); }
103
104public:
105
106 Timer()
107 {
108 mach_timebase_info(&base);
109 tick();
110 }
111
112 double
113 tock()
114 {
115 long long int nsec;
116
117 get_time(&now);
118 nsec = (now - then) * base.numer / base.denom;
119 return nsec*1e-9;
120 }
121
122 void tick() { get_time(&then); }
123};
124#endif /* MACOSX */
125
126#endif /* TIMER_HH_ */