Nvwa  1.1
pctimer.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 
4 /*
5  * Copyright (C) 2004-2015 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
37 #ifndef NVWA_PCTIMER_H
38 #define NVWA_PCTIMER_H
39 
40 #include "_nvwa.h" // NVWA_NAMESPACE_*/NVWA_WINDOWS
41 
42 #if NVWA_WINDOWS
43 
44 #ifndef _WIN32
45 #define NVWA_PCTIMER_NO_WIN32
46 #endif /* _WIN32 */
47 
48 #ifndef WIN32_LEAN_AND_MEAN
49 #define WIN32_LEAN_AND_MEAN
50 #endif /* WIN32_LEAN_AND_MEAN */
51 #include <windows.h> // QueryPerformance*
52 
53 #ifdef NVWA_PCTIMER_NO_WIN32
54 #undef NVWA_PCTIMER_NO_WIN32
55 #undef _WIN32
56 #endif /* NVWA_PCTIMER_NO_WIN32 */
57 
58 NVWA_NAMESPACE_BEGIN
59 
60 typedef double pctimer_t;
61 
62 __inline pctimer_t pctimer(void)
63 {
64  static LARGE_INTEGER pcount, pcfreq;
65  static int initflag;
66 
67  if (!initflag)
68  {
69  QueryPerformanceFrequency(&pcfreq);
70  initflag++;
71  }
72 
73  QueryPerformanceCounter(&pcount);
74  return (double)pcount.QuadPart / (double)pcfreq.QuadPart;
75 }
76 
77 NVWA_NAMESPACE_END
78 
79 #else /* Not Windows */
80 
81 #include <sys/time.h>
82 
83 NVWA_NAMESPACE_BEGIN
84 
85 typedef double pctimer_t;
86 
87 __inline pctimer_t pctimer(void)
88 {
89  struct timeval tv;
90  gettimeofday(&tv, NULL);
91  return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
92 }
93 
94 NVWA_NAMESPACE_END
95 
96 #endif /* NVWA_WINDOWS */
97 
98 #endif /* NVWA_PCTIMER_H */
Common definitions for preprocessing.