1 // Copyright (C)2004 Landmark Graphics Corporation
2 // Copyright (C)2005 Sun Microsystems, Inc.
3 // Copyright (C)2014, 2018-2019 D. R. Commander
4 //
5 // This library is free software and may be redistributed and/or modified under
6 // the terms of the wxWindows Library License, Version 3.1 or (at your option)
7 // any later version.  The full license is in the LICENSE.txt file included
8 // with this distribution.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // wxWindows Library License for more details.
14 
15 #ifndef __TIMER_H__
16 #define __TIMER_H__
17 
18 #ifdef _WIN32
19 #include <windows.h>
20 #else
21 #include <sys/time.h>
22 #endif
23 #include <stdlib.h>
24 #include "vglinline.h"
25 
26 
27 namespace vglutil
28 {
29 	class Timer
30 	{
31 		public:
32 
Timer(void)33 			Timer(void) : t1(0.0)
34 			{
35 				#ifdef _WIN32
36 				highRes = false;  tick = 0.001;
37 				LARGE_INTEGER frequency;
38 				if(QueryPerformanceFrequency(&frequency) != 0)
39 				{
40 					tick = (double)1.0 / (double)(frequency.QuadPart);
41 					highRes = true;
42 				}
43 				#endif
44 			}
45 
start(void)46 			void start(void)
47 			{
48 				t1 = time();
49 			}
50 
time(void)51 			double time(void)
52 			{
53 				#ifdef _WIN32
54 
55 				if(highRes)
56 				{
57 					LARGE_INTEGER Time;
58 					QueryPerformanceCounter(&Time);
59 					return (double)(Time.QuadPart) * tick;
60 				}
61 				else
62 					return (double)GetTickCount() * tick;
63 
64 				#else
65 
66 				struct timeval tv;
67 				gettimeofday(&tv, (struct timezone *)NULL);
68 				return (double)(tv.tv_sec) + (double)(tv.tv_usec) * 0.000001;
69 
70 				#endif
71 			}
72 
elapsed(void)73 			double elapsed(void)
74 			{
75 				return time() - t1;
76 			}
77 
78 		private:
79 
80 			#ifdef _WIN32
81 			bool highRes;  double tick;
82 			#endif
83 			double t1;
84 	};
85 }
86 
87 #endif  // __TIMER_H__
88