1/* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
2 All rights reserved.
3
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
9 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
11 3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 */
15#pragma once
16#ifndef VHACD_TIMER_H
17#define VHACD_TIMER_H
18
19#ifdef _WIN32
20#ifndef WIN32_LEAN_AND_MEAN
21#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
22#endif
23#include <windows.h>
24#elif __MACH__
25#include <mach/clock.h>
26#include <mach/mach.h>
27#else
28#include <sys/time.h>
29#include <time.h>
30#endif
31
32namespace VHACD {
33#ifdef _WIN32
34class Timer {
35public:
36 Timer(void)
37 {
38 m_start.QuadPart = 0;
39 m_stop.QuadPart = 0;
40 QueryPerformanceFrequency(&m_freq);
41 };
42 ~Timer(void){};
43 void Tic()
44 {
45 QueryPerformanceCounter(&m_start);
46 }
47 void Toc()
48 {
49 QueryPerformanceCounter(&m_stop);
50 }
51 double GetElapsedTime() // in ms
52 {
53 LARGE_INTEGER delta;
54 delta.QuadPart = m_stop.QuadPart - m_start.QuadPart;
55 return (1000.0 * delta.QuadPart) / (double)m_freq.QuadPart;
56 }
57
58private:
59 LARGE_INTEGER m_start;
60 LARGE_INTEGER m_stop;
61 LARGE_INTEGER m_freq;
62};
63
64#elif __MACH__
65class Timer {
66public:
67 Timer(void)
68 {
69 memset(this, 0, sizeof(Timer));
70 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &m_cclock);
71 };
72 ~Timer(void)
73 {
74 mach_port_deallocate(mach_task_self(), m_cclock);
75 };
76 void Tic()
77 {
78 clock_get_time(m_cclock, &m_start);
79 }
80 void Toc()
81 {
82 clock_get_time(m_cclock, &m_stop);
83 }
84 double GetElapsedTime() // in ms
85 {
86 return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
87 }
88
89private:
90 clock_serv_t m_cclock;
91 mach_timespec_t m_start;
92 mach_timespec_t m_stop;
93};
94#else
95class Timer {
96public:
97 Timer(void)
98 {
99 memset(this, 0, sizeof(Timer));
100 };
101 ~Timer(void){};
102 void Tic()
103 {
104 clock_gettime(CLOCK_REALTIME, &m_start);
105 }
106 void Toc()
107 {
108 clock_gettime(CLOCK_REALTIME, &m_stop);
109 }
110 double GetElapsedTime() // in ms
111 {
112 return 1000.0 * (m_stop.tv_sec - m_start.tv_sec + (1.0E-9) * (m_stop.tv_nsec - m_start.tv_nsec));
113 }
114
115private:
116 struct timespec m_start;
117 struct timespec m_stop;
118};
119#endif
120}
121#endif // VHACD_TIMER_H
122