1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
//
// MIT License
// Copyright (c) 2019 Jonathan R. Madsen
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// ----------------------------------------------------------------------
// Class Timer
//
// Class description:
//
// Class for timer objects, able to measure elasped user/system process time.
//
// Note: Uses <sys/times.h> & <unistd.h> - POSIX.1 defined
// If used, this header must be included in the source (.cc) file and it
// must be the first header file to be included!
//
// Member functions:
//
// Timer()
// Construct a timer object
// Start()
// Start timing
// Stop()
// Stop timing
// bool IsValid()
// Return true if have a valid time (ie start() and stop() called)
// double GetRealElapsed()
// Return the elapsed real time between last calling start() and stop()
// double GetSystemElapsed()
// Return the elapsed system time between last calling start() and stop()
// double GetUserElapsed()
// Return the elapsed user time between last calling start() and stop()
//
// Operators:
//
// std::ostream& operator << (std::ostream& os, const Timer& t);
// Print the elapsed real,system and usertimes on os. Prints **s for times
// if !IsValid
//
// Member data:
//
// bool fValidTimes
// True after start and stop have both been called more than once and
// an equal number of times
// clock_t fStartRealTime,fEndRealTime
// Real times (arbitrary time 0)
// tms fStartTimes,fEndTimes
// Timing structures (see times(2)) for start and end times
// History:
// 23.08.96 P.Kent Updated to also computed real elapsed time
// 21.08.95 P.Kent
// 29.04.97 G.Cosmo Added timings for Windows/NT
#pragma once
#include "PTL/Types.hh"
#include <chrono>
#include <iomanip>
#include <ostream>
#include <sstream>
#if !(defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64))
# include <sys/times.h>
# include <unistd.h>
#else
# include <ctime>
# define _SC_CLK_TCK 1
extern "C"
{
int sysconf(int);
};
// Structure returned by times()
struct tms
{
clock_t tms_utime; /* user time */
clock_t tms_stime; /* system time */
clock_t tms_cutime; /* user time, children */
clock_t tms_cstime; /* system time, children */
};
extern "C"
{
extern clock_t times(struct tms*);
};
#endif /* WIN32 */
namespace PTL
{
class Timer
{
typedef std::chrono::high_resolution_clock clock_type;
public:
Timer();
public:
inline void Start();
inline void Stop();
inline bool IsValid() const;
inline const char* GetClockTime() const;
double GetRealElapsed() const;
double GetSystemElapsed() const;
double GetUserElapsed() const;
public:
friend std::ostream& operator<<(std::ostream& os, const Timer& t)
{
// so fixed doesn't propagate
std::stringstream ss;
ss << std::fixed;
if(t.IsValid())
{
ss << "Real=" << t.GetRealElapsed() << "s User=" << t.GetUserElapsed()
<< "s Sys=" << t.GetSystemElapsed() << "s";
// avoid possible FPE error
if(t.GetRealElapsed() > 1.0e-6)
{
double cpu_util = (t.GetUserElapsed() + t.GetSystemElapsed()) /
t.GetRealElapsed() * 100.0;
ss << std::setprecision(1);
ss << " [Cpu=" << std::setprecision(1) << cpu_util << "%]";
}
}
else
{
ss << "Real=****s User=****s Sys=****s";
}
os << ss.str();
return os;
}
private:
bool fValidTimes;
std::chrono::time_point<clock_type> fStartRealTime, fEndRealTime;
tms fStartTimes, fEndTimes;
};
} // namespace PTL
#include "PTL/Timer.icc"
|