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
/***************************************************************************
* Simple Stopwatch Library
*
* File : stopwatch.h
* Purpose : Header for library implementing basic stopwatch functionality.
* Contains type definitions, prototypes, and a class definition
* to be used by programs linking to the Stopwatch library.
* Author : Michael Dipperstein
* Date : July 7, 2011
*
****************************************************************************
*
* Stopwatch: A POSIX C/C++ Stopwatch Library
* Copyright (C) 2011 by Michael Dipperstein (mdipper@alumni.cs.ucsb.edu)
*
* This file is part of the stopwatch library.
*
* The stopwatch library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* The stopwatch library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
#ifndef STOPWATCH_H
#define STOPWATCH_H
/***************************************************************************
* INCLUDED FILES
***************************************************************************/
#include <time.h>
/***************************************************************************
* CONSTANTS
***************************************************************************/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/***************************************************************************
* TYPE DEFINITIONS
***************************************************************************/
typedef struct
{
unsigned char isRunning;
struct timespec startTime;
struct timespec runningTime;
} stopwatch_t;
#if defined __cplusplus
extern "C"
{
#endif
/***************************************************************************
* PROTOTYPES
***************************************************************************/
void StartTimer(stopwatch_t *stopWatch); /* start or restart stopwatch */
void StopTimer(stopwatch_t *stopWatch); /* stopwatch */
void ResumeTimer(stopwatch_t *stopWatch); /* resume stopped watch */
/* returns stopwatch time in ms */
unsigned long ReadTimer(const stopwatch_t *stopWatch);
/* delay a started stopwatch until end seconds */
void DelayTimer(const stopwatch_t *stopWatch, double end);
#if defined __cplusplus
}
#endif
#if defined __cplusplus
/* c++ class encapsulations */
/***************************************************************************
* CLASS DEFINITIONS
***************************************************************************/
class stopwatch_c
{
public:
stopwatch_c(void)
{
watchData.isRunning = FALSE;
watchData.startTime.tv_sec = 0;
watchData.startTime.tv_nsec = 0;
watchData.runningTime.tv_sec = 0;
watchData.runningTime.tv_nsec = 0;
};
~stopwatch_c(void) {};
void Start(void) {StartTimer(&watchData);};
void Stop(void) {StopTimer(&watchData);};
void Resume(void) {ResumeTimer(&watchData);};
void Delay(double end) {DelayTimer(&watchData, end);};
/* running time in ms last time if stopped */
unsigned long Read(void) const
{
return ReadTimer(&watchData);
};
private:
stopwatch_t watchData;
};
#endif /* defined __cplusplus */
#endif /* ndef STOPWATCH_H */