| [658] | 1 | /*************************************************************************** | 
|---|
|  | 2 | * blitz/Timer.h        Timer class, for use in benchmarking | 
|---|
|  | 3 | * | 
|---|
|  | 4 | * $Id: timer.h,v 1.1.1.1 1999-11-26 16:37:04 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.1.1.1  1999/04/09  17:58:59  ansari | 
|---|
|  | 27 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99 | 
|---|
|  | 28 | * | 
|---|
|  | 29 | * Revision 1.4  1998/03/14 00:04:47  tveldhui | 
|---|
|  | 30 | * 0.2-alpha-05 | 
|---|
|  | 31 | * | 
|---|
|  | 32 | * Revision 1.3  1997/07/16 14:51:20  tveldhui | 
|---|
|  | 33 | * Update: Alpha release 0.2 (Arrays) | 
|---|
|  | 34 | * | 
|---|
|  | 35 | * Revision 1.2  1997/01/24 14:42:00  tveldhui | 
|---|
|  | 36 | * Periodic RCS update | 
|---|
|  | 37 | * | 
|---|
|  | 38 | */ | 
|---|
|  | 39 |  | 
|---|
|  | 40 | // This class is not portable to non System V platforms. | 
|---|
|  | 41 | // It will need to be rewritten for Windows, NT, Mac. | 
|---|
|  | 42 | // NEEDS_WORK | 
|---|
|  | 43 |  | 
|---|
|  | 44 | #ifndef BZ_TIMER_H | 
|---|
|  | 45 | #define BZ_TIMER_H | 
|---|
|  | 46 |  | 
|---|
|  | 47 | #ifndef BZ_BLITZ_H | 
|---|
|  | 48 | #include <blitz/blitz.h> | 
|---|
|  | 49 | #endif | 
|---|
|  | 50 |  | 
|---|
|  | 51 | #ifdef BZ_HAVE_RUSAGE | 
|---|
|  | 52 | #include <sys/resource.h> | 
|---|
|  | 53 | #else | 
|---|
|  | 54 | #include <time.h> | 
|---|
|  | 55 | #endif | 
|---|
|  | 56 |  | 
|---|
|  | 57 | BZ_NAMESPACE(blitz) | 
|---|
|  | 58 |  | 
|---|
|  | 59 | class Timer { | 
|---|
|  | 60 |  | 
|---|
|  | 61 | public: | 
|---|
|  | 62 | Timer() | 
|---|
|  | 63 | { | 
|---|
|  | 64 | state_ = uninitialized; | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | void start() | 
|---|
|  | 68 | { | 
|---|
|  | 69 | state_ = running; | 
|---|
|  | 70 | t1_ = systemTime(); | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
|  | 73 | void stop() | 
|---|
|  | 74 | { | 
|---|
|  | 75 | t2_ = systemTime(); | 
|---|
|  | 76 | BZPRECONDITION(state_ == running); | 
|---|
|  | 77 | state_ = stopped; | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 | long double elapsedSeconds() | 
|---|
|  | 81 | { | 
|---|
|  | 82 | BZPRECONDITION(state_ == stopped); | 
|---|
|  | 83 | return t2_ - t1_; | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
|  | 86 | private: | 
|---|
|  | 87 | Timer(Timer&) { } | 
|---|
|  | 88 | void operator=(Timer&) { } | 
|---|
|  | 89 |  | 
|---|
|  | 90 | long double systemTime() | 
|---|
|  | 91 | { | 
|---|
|  | 92 | #ifdef BZ_HAVE_RUSAGE | 
|---|
|  | 93 | getrusage(RUSAGE_SELF, &resourceUsage_); | 
|---|
|  | 94 | double seconds = resourceUsage_.ru_utime.tv_sec | 
|---|
|  | 95 | + resourceUsage_.ru_stime.tv_sec; | 
|---|
|  | 96 | double micros  = resourceUsage_.ru_utime.tv_usec | 
|---|
|  | 97 | + resourceUsage_.ru_stime.tv_usec; | 
|---|
|  | 98 | return seconds + micros/1.0e6; | 
|---|
|  | 99 | #else | 
|---|
|  | 100 | return clock() / (long double) CLOCKS_PER_SEC; | 
|---|
|  | 101 | #endif | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | enum { uninitialized, running, stopped } state_; | 
|---|
|  | 105 |  | 
|---|
|  | 106 | #ifdef BZ_HAVE_RUSAGE | 
|---|
|  | 107 | struct rusage resourceUsage_; | 
|---|
|  | 108 | #endif | 
|---|
|  | 109 |  | 
|---|
|  | 110 | long double t1_, t2_; | 
|---|
|  | 111 | }; | 
|---|
|  | 112 |  | 
|---|
|  | 113 | BZ_NAMESPACE_END | 
|---|
|  | 114 |  | 
|---|
|  | 115 | #endif // BZ_TIMER_H | 
|---|
|  | 116 |  | 
|---|