xref: /haiku/src/kits/support/StopWatch.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 // 100% done
2 #include "support/StopWatch.h"
3 #include <OS.h>
4 #include <stdio.h>
5 
6 #ifdef USE_OPENBEOS_NAMESPACE
7 namespace OpenBeOS {
8 #endif
9 
10 BStopWatch::BStopWatch(const char *name, bool silent){
11 	fSilent = silent;
12 	fName = name;
13 	Reset();
14 }
15 
16 BStopWatch::~BStopWatch(){
17 	if (!fSilent){
18 		printf("StopWatch \"%s\": %d usecs.", fName, (int)ElapsedTime() );
19 
20 		if (fLap){
21 			for (int i=1; i<=fLap; i++){
22 				if (!((i-1)%4))	printf("\n   ");
23 				printf("[%d: %d#%d] ", i, (int)(fLaps[i]-fStart), (int)(fLaps[i] - fLaps[i-1]) );
24 			}
25 			printf("\n");
26 		}
27 	}
28 }
29 
30 void BStopWatch::Suspend(){
31 	if (!fSuspendTime)
32 		fSuspendTime = system_time();
33 }
34 
35 void BStopWatch::Resume(){
36 	if (fSuspendTime)
37 		fStart = system_time() - fSuspendTime - fStart;
38 }
39 
40 bigtime_t BStopWatch::Lap(){
41 	if (!fSuspendTime){
42 		if (fLap<9) fLap++;
43 		fLaps[fLap] = system_time();
44 		return (system_time()-fStart);
45 	}else
46 		return 0;
47 }
48 
49 bigtime_t BStopWatch::ElapsedTime() const{
50 	if (fSuspendTime)
51 		return (fSuspendTime-fStart);
52 	else
53 		return (system_time()-fStart);
54 }
55 
56 void BStopWatch::Reset(){
57 	fStart = system_time();		// store current time
58 	fSuspendTime = 0;
59 	fLap = 0;					// clear laps
60 	for (int i=0; i<10; i++)
61 		fLaps[i] = fStart;
62 }
63 
64 const char *BStopWatch::Name() const{
65 	return fName;
66 }
67 
68 // just for future binary compatibility
69 void BStopWatch::_ReservedStopWatch1()	{}
70 void BStopWatch::_ReservedStopWatch2()	{}
71 
72 #ifdef USE_OPENBEOS_NAMESPACE
73 }	// namespace OpenBeOS
74 #endif
75