1 | /*************************************************************************** |
---|
2 | * blitz/Timer.h Timer class, for use in benchmarking |
---|
3 | * |
---|
4 | * $Id: timer.h,v 1.4 2005/10/10 22:35:30 julianc Exp $ |
---|
5 | * |
---|
6 | * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org> |
---|
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-dev@oonumerics.org |
---|
19 | * Bugs: blitz-bugs@oonumerics.org |
---|
20 | * |
---|
21 | * For more information, please see the Blitz++ Home Page: |
---|
22 | * http://oonumerics.org/blitz/ |
---|
23 | * |
---|
24 | ***************************************************************************/ |
---|
25 | |
---|
26 | // This class is not portable to non System V platforms. |
---|
27 | // It will need to be rewritten for Windows, NT, Mac. |
---|
28 | // NEEDS_WORK |
---|
29 | |
---|
30 | #ifndef BZ_TIMER_H |
---|
31 | #define BZ_TIMER_H |
---|
32 | |
---|
33 | #ifndef BZ_BLITZ_H |
---|
34 | #include <blitz/blitz.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | #ifdef BZ_HAVE_RUSAGE |
---|
38 | #include <sys/resource.h> |
---|
39 | #else |
---|
40 | #include <time.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | BZ_NAMESPACE(blitz) |
---|
44 | |
---|
45 | class Timer { |
---|
46 | |
---|
47 | public: |
---|
48 | Timer() |
---|
49 | { |
---|
50 | state_ = uninitialized; |
---|
51 | } |
---|
52 | |
---|
53 | void start() |
---|
54 | { |
---|
55 | state_ = running; |
---|
56 | t1_ = systemTime(); |
---|
57 | } |
---|
58 | |
---|
59 | void stop() |
---|
60 | { |
---|
61 | t2_ = systemTime(); |
---|
62 | BZPRECONDITION(state_ == running); |
---|
63 | state_ = stopped; |
---|
64 | } |
---|
65 | |
---|
66 | /* Compaq cxx compiler in ansi mode cannot print out long double type! */ |
---|
67 | #if defined(__DECCXX) |
---|
68 | double elapsedSeconds() |
---|
69 | #else |
---|
70 | long double elapsedSeconds() |
---|
71 | #endif |
---|
72 | { |
---|
73 | BZPRECONDITION(state_ == stopped); |
---|
74 | return t2_ - t1_; |
---|
75 | } |
---|
76 | |
---|
77 | private: |
---|
78 | Timer(Timer&) { } |
---|
79 | void operator=(Timer&) { } |
---|
80 | |
---|
81 | long double systemTime() |
---|
82 | { |
---|
83 | #ifdef BZ_HAVE_RUSAGE |
---|
84 | getrusage(RUSAGE_SELF, &resourceUsage_); |
---|
85 | double seconds = resourceUsage_.ru_utime.tv_sec |
---|
86 | + resourceUsage_.ru_stime.tv_sec; |
---|
87 | double micros = resourceUsage_.ru_utime.tv_usec |
---|
88 | + resourceUsage_.ru_stime.tv_usec; |
---|
89 | return seconds + micros/1.0e6; |
---|
90 | #else |
---|
91 | return clock() / (long double) CLOCKS_PER_SEC; |
---|
92 | #endif |
---|
93 | } |
---|
94 | |
---|
95 | enum { uninitialized, running, stopped } state_; |
---|
96 | |
---|
97 | #ifdef BZ_HAVE_RUSAGE |
---|
98 | struct rusage resourceUsage_; |
---|
99 | #endif |
---|
100 | |
---|
101 | long double t1_, t2_; |
---|
102 | }; |
---|
103 | |
---|
104 | BZ_NAMESPACE_END |
---|
105 | |
---|
106 | #endif // BZ_TIMER_H |
---|
107 | |
---|