Knight's Tour Generator
Tourneys and the Fast Generation and Obfuscation of Closed Knight's Tours
Timer.cpp
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2019 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #define _CRT_SECURE_NO_WARNINGS
27 
28 #include "Timer.h"
29 
31 
33  m_tpElapsedTimeStart = sysclock::now(); //time_point
34  m_fCPUTimeStart = CPUTime()/1000.0f;
35 } //Start
36 
42 
44  const time_t t = sysclock::to_time_t(p);
45  m_strTimeAndDate = ctime(&t);
46  return m_strTimeAndDate.c_str();
47 } //GetStartDateAndTime
48 
52 
55 } //GetStartDateAndTime
56 
60 
62  return GetDateAndTime(sysclock::now());
63 } //GetCurrentDateAndTime
64 
67 
69  std::chrono::duration<float> dt = sysclock::now() - m_tpElapsedTimeStart;
70  return dt.count();
71 } //GetElapsedTime
72 
75 
77  return CPUTime()/1000.0f - m_fCPUTimeStart;
78 } //GetCPUTime
79 
82 
84  printf("Finished at: %s", GetCurrentDateAndTime());
85  printf("Elapsed time %0.1f sec, CPU time %0.1f sec\n",
87 } //Finish
88 
92 
93 #if defined(_MSC_VER) //Windows and Visual Studio
94 
96  UINT64 llCNS = 0; //for CPU time in centinanoseconds
97  const DWORD pid = GetCurrentProcessId(); //process id
98  HANDLE hp = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); //proc handle
99 
100  if(hp != nullptr){ //handle is valid
101  FILETIME ft0, ft1, ft2; //unused
102  FILETIME ftUser; //this one we want
103 
104  if(GetProcessTimes(hp, &ft0, &ft1, &ft2, &ftUser))
105  llCNS = *((UINT64*)&ftUser); //get time values
106 
107  CloseHandle(hp); //close the process handle
108  } //if
109 
110  return UINT(llCNS/10000ULL); //round to milliseconds
111  } //CPUTime
112 
113 #else //*nix and g++
114 
115  #include <time.h>
116 
117  UINT CTimer::CPUTime(){ //*nix version
118  return UINT(1000.0*(double)(clock())/CLOCKS_PER_SEC);
119  } //UINT
120 
121 #endif
122 
std::chrono::time_point< sysclock > systime_point
System time point.
Definition: Timer.h:33
const char * GetCurrentDateAndTime()
Get current time and date.
Definition: Timer.cpp:61
systime_point m_tpElapsedTimeStart
Clock time.
Definition: Timer.h:45
uint64_t UINT64
Typedef of UINT64 for *NIX.
Definition: Defines.h:38
UINT CPUTime()
Platform independent CPU time.
Definition: Timer.cpp:117
const char * GetDateAndTime(const systime_point p)
Get time and date.
Definition: Timer.cpp:43
const char * GetStartDateAndTime()
Get start time and date.
Definition: Timer.cpp:53
void Start()
Start timing.
Definition: Timer.cpp:32
Header for the timer CTimer.
unsigned int UINT
Abbreviation for unsigned integer.
Definition: Defines.h:84
float GetCPUTime()
Get CPU time in seconds.
Definition: Timer.cpp:76
void Finish()
Print CPU and elapsed time.
Definition: Timer.cpp:83
std::string m_strTimeAndDate
Container for time and date string.
Definition: Timer.h:47
float GetElapsedTime()
Get elapsed time in seconds.
Definition: Timer.cpp:68
float m_fCPUTimeStart
CPU time in seconds.
Definition: Timer.h:46