]> git.sesse.net Git - casparcg/blob - common/utility/timer.h
2.0.0.2: Added copyright notice to all files.
[casparcg] / common / utility / timer.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #pragma once\r
21 \r
22 #include <windows.h>\r
23 \r
24 namespace caspar {\r
25         \r
26 class timer\r
27 {\r
28 public:\r
29         timer() : time_(timeGetTime()){}\r
30 \r
31         double elapsed()\r
32         {\r
33                 return static_cast<double>(timeGetTime() - time_)/1000.0;\r
34         }\r
35         \r
36         void reset()\r
37         {\r
38                 time_ = timeGetTime();\r
39         }\r
40         \r
41 private:        \r
42         DWORD time_;\r
43 };\r
44 \r
45 class high_prec_timer\r
46 {\r
47 public:\r
48         high_prec_timer()\r
49         {\r
50                 QueryPerformanceFrequency(&freq_);\r
51                 QueryPerformanceCounter(&time_);\r
52         }\r
53 \r
54         // Author: Ryan M. Geiss\r
55         // http://www.geisswerks.com/ryan/FAQS/timing.html\r
56         void tick(double interval)\r
57         {       \r
58                 LARGE_INTEGER t;\r
59                 QueryPerformanceCounter(&t);\r
60 \r
61                 if (time_.QuadPart != 0)\r
62                 {\r
63                         int ticks_to_wait = static_cast<int>(static_cast<double>(freq_.QuadPart) * interval);\r
64                         int done = 0;\r
65                         do\r
66                         {\r
67                                 QueryPerformanceCounter(&t);\r
68                                 \r
69                                 int ticks_passed = static_cast<int>(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart));\r
70                                 int ticks_left = ticks_to_wait - ticks_passed;\r
71 \r
72                                 if (t.QuadPart < time_.QuadPart)    // time wrap\r
73                                         done = 1;\r
74                                 if (ticks_passed >= ticks_to_wait)\r
75                                         done = 1;\r
76                                 \r
77                                 if (!done)\r
78                                 {\r
79                                         // if > 0.002s left, do Sleep(1), which will actually sleep some \r
80                                         //   steady amount, probably 1-2 ms,\r
81                                         //   and do so in a nice way (cpu meter drops; laptop battery spared).\r
82                                         // otherwise, do a few Sleep(0)'s, which just give up the timeslice,\r
83                                         //   but don't really save cpu or battery, but do pass a tiny\r
84                                         //   amount of time.\r
85                                         if (ticks_left > static_cast<int>((freq_.QuadPart*2)/1000))\r
86                                                 Sleep(1);\r
87                                         else                        \r
88                                                 for (int i = 0; i < 10; ++i) \r
89                                                         Sleep(0);  // causes thread to give up its timeslice\r
90                                 }\r
91                         }\r
92                         while (!done);            \r
93                 }\r
94 \r
95                 time_ = t;\r
96         }               \r
97 private:        \r
98         LARGE_INTEGER freq_;\r
99         LARGE_INTEGER time_;\r
100 };\r
101 \r
102 \r
103 }