xref: /haiku/src/kits/support/StopWatch.cpp (revision 81f5654c124bf46fba0fd251f208e2d88d81e1ce)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		StopWatch.cpp
23 //	Author(s):		unknown
24 //
25 //	Description:	Timer class
26 //
27 //
28 //------------------------------------------------------------------------------
29 #include <StopWatch.h>
30 #include <OS.h>
31 #include <stdio.h>
32 
33 #ifdef USE_OPENBEOS_NAMESPACE
34 namespace OpenBeOS {
35 #endif
36 
37 BStopWatch::BStopWatch(const char *name, bool silent){
38 	fSilent = silent;
39 	fName = name;
40 	Reset();
41 }
42 
43 BStopWatch::~BStopWatch(){
44 	if (!fSilent){
45 		printf("StopWatch \"%s\": %d usecs.", fName, (int)ElapsedTime() );
46 
47 		if (fLap){
48 			for (int i=1; i<=fLap; i++){
49 				if (!((i-1)%4))	printf("\n   ");
50 				printf("[%d: %d#%d] ", i, (int)(fLaps[i]-fStart), (int)(fLaps[i] - fLaps[i-1]) );
51 			}
52 			printf("\n");
53 		}
54 	}
55 }
56 
57 void BStopWatch::Suspend(){
58 	if (!fSuspendTime)
59 		fSuspendTime = system_time();
60 }
61 
62 void BStopWatch::Resume(){
63 	if (fSuspendTime)
64 		fStart = system_time() - fSuspendTime - fStart;
65 }
66 
67 bigtime_t BStopWatch::Lap(){
68 	if (!fSuspendTime){
69 		if (fLap<9) fLap++;
70 		fLaps[fLap] = system_time();
71 		return (system_time()-fStart);
72 	}else
73 		return 0;
74 }
75 
76 bigtime_t BStopWatch::ElapsedTime() const{
77 	if (fSuspendTime)
78 		return (fSuspendTime-fStart);
79 	else
80 		return (system_time()-fStart);
81 }
82 
83 void BStopWatch::Reset(){
84 	fStart = system_time();		// store current time
85 	fSuspendTime = 0;
86 	fLap = 0;					// clear laps
87 	for (int i=0; i<10; i++)
88 		fLaps[i] = fStart;
89 }
90 
91 const char *BStopWatch::Name() const{
92 	return fName;
93 }
94 
95 // just for future binary compatibility
96 void BStopWatch::_ReservedStopWatch1()	{}
97 void BStopWatch::_ReservedStopWatch2()	{}
98 
99 #ifdef USE_OPENBEOS_NAMESPACE
100 }	// namespace OpenBeOS
101 #endif
102