1 | /***************************************************************************
|
---|
2 | * blitz/Timer.h Timer class, for use in benchmarking
|
---|
3 | *
|
---|
4 | * $Id: timer.h,v 1.1.1.1 1999-04-09 17:58:59 ansari Exp $
|
---|
5 | *
|
---|
6 | * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
|
---|
7 | *
|
---|
8 | * This program is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU General Public License
|
---|
10 | * as published by the Free Software Foundation; either version 2
|
---|
11 | * of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This program is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * Suggestions: blitz-suggest@cybervision.com
|
---|
19 | * Bugs: blitz-bugs@cybervision.com
|
---|
20 | *
|
---|
21 | * For more information, please see the Blitz++ Home Page:
|
---|
22 | * http://seurat.uwaterloo.ca/blitz/
|
---|
23 | *
|
---|
24 | ***************************************************************************
|
---|
25 | * $Log: not supported by cvs2svn $
|
---|
26 | * Revision 1.4 1998/03/14 00:04:47 tveldhui
|
---|
27 | * 0.2-alpha-05
|
---|
28 | *
|
---|
29 | * Revision 1.3 1997/07/16 14:51:20 tveldhui
|
---|
30 | * Update: Alpha release 0.2 (Arrays)
|
---|
31 | *
|
---|
32 | * Revision 1.2 1997/01/24 14:42:00 tveldhui
|
---|
33 | * Periodic RCS update
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | // This class is not portable to non System V platforms.
|
---|
38 | // It will need to be rewritten for Windows, NT, Mac.
|
---|
39 | // NEEDS_WORK
|
---|
40 |
|
---|
41 | #ifndef BZ_TIMER_H
|
---|
42 | #define BZ_TIMER_H
|
---|
43 |
|
---|
44 | #ifndef BZ_BLITZ_H
|
---|
45 | #include <blitz/blitz.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #ifdef BZ_HAVE_RUSAGE
|
---|
49 | #include <sys/resource.h>
|
---|
50 | #else
|
---|
51 | #include <time.h>
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | BZ_NAMESPACE(blitz)
|
---|
55 |
|
---|
56 | class Timer {
|
---|
57 |
|
---|
58 | public:
|
---|
59 | Timer()
|
---|
60 | {
|
---|
61 | state_ = uninitialized;
|
---|
62 | }
|
---|
63 |
|
---|
64 | void start()
|
---|
65 | {
|
---|
66 | state_ = running;
|
---|
67 | t1_ = systemTime();
|
---|
68 | }
|
---|
69 |
|
---|
70 | void stop()
|
---|
71 | {
|
---|
72 | t2_ = systemTime();
|
---|
73 | BZPRECONDITION(state_ == running);
|
---|
74 | state_ = stopped;
|
---|
75 | }
|
---|
76 |
|
---|
77 | long double elapsedSeconds()
|
---|
78 | {
|
---|
79 | BZPRECONDITION(state_ == stopped);
|
---|
80 | return t2_ - t1_;
|
---|
81 | }
|
---|
82 |
|
---|
83 | private:
|
---|
84 | Timer(Timer&) { }
|
---|
85 | void operator=(Timer&) { }
|
---|
86 |
|
---|
87 | long double systemTime()
|
---|
88 | {
|
---|
89 | #ifdef BZ_HAVE_RUSAGE
|
---|
90 | getrusage(RUSAGE_SELF, &resourceUsage_);
|
---|
91 | double seconds = resourceUsage_.ru_utime.tv_sec
|
---|
92 | + resourceUsage_.ru_stime.tv_sec;
|
---|
93 | double micros = resourceUsage_.ru_utime.tv_usec
|
---|
94 | + resourceUsage_.ru_stime.tv_usec;
|
---|
95 | return seconds + micros/1.0e6;
|
---|
96 | #else
|
---|
97 | return clock() / (long double) CLOCKS_PER_SEC;
|
---|
98 | #endif
|
---|
99 | }
|
---|
100 |
|
---|
101 | enum { uninitialized, running, stopped } state_;
|
---|
102 |
|
---|
103 | #ifdef BZ_HAVE_RUSAGE
|
---|
104 | struct rusage resourceUsage_;
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | long double t1_, t2_;
|
---|
108 | };
|
---|
109 |
|
---|
110 | BZ_NAMESPACE_END
|
---|
111 |
|
---|
112 | #endif // BZ_TIMER_H
|
---|
113 |
|
---|