Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ACGL
acgl
Commits
28cd1395
Commit
28cd1395
authored
Aug 05, 2011
by
Robert Menzel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added performance timers
parent
3541659a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
79 additions
and
0 deletions
+79
-0
include/ACGL/Utils/PerformanceTimer.hh
include/ACGL/Utils/PerformanceTimer.hh
+79
-0
No files found.
include/ACGL/Utils/PerformanceTimer.hh
0 → 100644
View file @
28cd1395
#ifndef PERFORMANCETIMER_HH
#define PERFORMANCETIMER_HH
#include "../ACGL.hh"
#include <iostream>
#include <ctime>
/*
* Three Timers are defined here: SystemTimer, ProcessTimer and ThreadTimer.
* They can be used to do basic time measurements with high accuracy.
*
* SystemTimer is based on the time since 1.1.1970.
* ProcessTime is based on the runtime of the process/application.
* ThreadTime is based on the time the thread has run in which it is called.
*
* Just use it like:
*
* ProcessTime t; // automatic reset
* slowFunction();
* cout << "function took " << t.getTimeInSecondsD() << " seconds";
* t.reset();
* fastFunction();
* cout << "other function took " << t.getTimeInNanoseconds() << " nanoseconds";
*/
namespace
ACGL
{
namespace
Utils
{
template
<
clockid_t
TIMER_TYPE
>
class
PerformanceTimer
{
public:
PerformanceTimer
()
{
restart
();
}
// set the mStartTime to the current time:
inline
void
restart
()
{
timespec
t
;
clock_gettime
(
TIMER_TYPE
,
&
t
);
mStartTime
=
timespecTo64
(
t
);
}
// returns the CPU/Process/Thread time in nanosecounds, a 64 bit unsigned int will overflow after
// 584 years...
inline
uint64_t
getTimeInNanoseconds
()
{
timespec
t
;
clock_gettime
(
TIMER_TYPE
,
&
t
);
return
timespecTo64
(
t
);
}
// 32 bit unsigned int with seconds will overflow after 136 years
inline
uint32_t
getTimeInSeconds
()
{
timespec
t
;
clock_gettime
(
TIMER_TYPE
,
&
t
);
return
t
.
tv_sec
;
}
inline
double
getTimeInSecondsD
()
{
timespec
t
;
clock_gettime
(
TIMER_TYPE
,
&
t
);
return
timespecToDouble
(
t
);
}
// get time diff since last reset:
inline
uint64_t
getTimeDiffInNanoseconds
()
{
return
getTimeInNanoseconds
()
-
mStartTime
;
}
// 32 bit unsigned int with seconds will overflow after 136 years
inline
uint32_t
getTimeDiffInSeconds
()
{
return
getTimeInSeconds
()
-
(
mStartTime
/
1000000000
);
}
inline
double
getTimeDiffInSecondsD
()
{
return
getTimeInSecondsD
()
-
(
mStartTime
/
1000000000.0
);
}
// get the system dependent resolution of the timing in nanoseconds
inline
uint64_t
getTimerResolutionInNanoseconds
()
{
timespec
t
;
clock_getres
(
TIMER_TYPE
,
&
t
);
return
timespecTo64
(
t
);
}
private:
inline
uint64_t
timespecTo64
(
const
timespec
&
_time
)
const
{
return
(
_time
.
tv_sec
*
1000000000
+
_time
.
tv_nsec
);
}
inline
uint64_t
timespecToDouble
(
const
timespec
&
_time
)
const
{
return
(
_time
.
tv_sec
+
_time
.
tv_nsec
/
1000000000.0
);
}
uint64_t
mStartTime
;
};
// systemwide clock which holds the time since 1.1.1970:
typedef
PerformanceTimer
<
((
clockid_t
)
CLOCK_REALTIME
)
>
SystemTimer
;
typedef
PerformanceTimer
<
((
clockid_t
)
CLOCK_PROCESS_CPUTIME_ID
)
>
ProcessTimer
;
typedef
PerformanceTimer
<
((
clockid_t
)
CLOCK_THREAD_CPUTIME_ID
)
>
ThreadTimer
;
}
// Utils
}
// ACGL
#endif // PERFORMANCETIMER_HH
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment