-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCProfileTimer.h
98 lines (80 loc) · 2.74 KB
/
CProfileTimer.h
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
/*
Copyright [2010] [Richard Bross]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* File: CProfileTimer.h
* Author: rbross
*
* Created on April 11, 2012, 8:36 AM
*/
#ifndef CPROFILETIMER_H
#define CPROFILETIMER_H
#include <time.h>
#include <sys/time.h>
#include <map>
#include <iostream>
#include <string>
using namespace std;
typedef map<string, struct timeval> MAP_TIMES;
typedef MAP_TIMES::iterator MAP_TIMES_ITER;
//! Class to calculate elapsed time
class ElapsedTime
{
public:
ElapsedTime()
{
lSec = 0;
lMS = 0;
lUS = 0;
};
ElapsedTime(const ElapsedTime& orig)
{
lSec = orig.lSec;
lMS = orig.lMS;
lUS = orig.lUS;
};
virtual ~ElapsedTime() {};
long lSec;
long lMS;
long lUS;
};
class CProfileTimer
{
public:
CProfileTimer();
virtual ~CProfileTimer();
// Non static call without labels to start a timer. For very time sensitive processes, this avoids using the map
void Start();
// Get the elapsed time, if bReset is true reset the start time
void GetElapsedTime(ElapsedTime &cElapsed, bool bReset = true);
// Set and store a timestamp
static void SetTimestamp(const char *pLabel);
// Set the second stamp and get the difference at the same time. If bMarkLabel2 is true, save current time for label 2
static void GetElapsed(ElapsedTime &cElapsed, const char *pLabel1, const char *pLabel2, bool bMarkLabel2 = false);
// Get the elapsed time from start until now
static void GetElapsedNow(ElapsedTime &cElapsed, const char *pLabel1);
// Set the second, get the difference and print
static void PrintElapsed(const char *pLabel1, const char *pLabel2, bool bMarkLabel2 = false, const char *pTag = NULL);
// Return just the number of ms
static long GetElapsedMS(const char *pLabel1, const char *pLabel2, bool bMarkLabel2 = false);
// Clear the map
static void Clear();
protected:
static struct timeval GetLabelTimestamp(const char *pLabel);
static ElapsedTime GetDifference(ElapsedTime &cElapsed, struct timeval *sT1, struct timeval *sT2);
protected:
struct timeval sStart;
struct timeval sEnd;
private:
static MAP_TIMES mTimes;
};
#endif /* CPROFILETIMER_H */