00001 /* 00002 * Copyright 1997, Brown University, Providence, RI. 00003 * 00004 * All Rights Reserved 00005 * 00006 * Permission to use, copy, modify, and distribute this software and its 00007 * documentation for any purpose other than its incorporation into a 00008 * commercial product is hereby granted without fee, provided that the 00009 * above copyright notice appear in all copies and that both that 00010 * copyright notice and this permission notice appear in supporting 00011 * documentation, and that the name of Brown University not be used in 00012 * advertising or publicity pertaining to distribution of the software 00013 * without specific, written prior permission. 00014 * 00015 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 00016 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY 00017 * PARTICULAR PURPOSE. IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR 00018 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00019 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00020 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00021 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00022 */ 00023 /* Copyright 1997, Brown Computer Graphics Group. All Rights Reserved. */ 00024 00025 /********************************************************************** 00026 * stop_watch.H 00027 **********************************************************************/ 00028 #ifndef STOP_WATCH_H_HAS_BEEN_INCLUDED 00029 #define STOP_WATCH_H_HAS_BEEN_INCLUDED 00030 00031 #ifdef sun 00032 #include <sys/time.h> // gettimeofday, gethrtime 00033 #endif 00034 #include "std/time.H" 00035 #include "std/error.H" // io etc. 00036 00037 /********************************************************************** 00038 * stop_watch: 00039 * 00040 * utility class for getting or printing timing info. 00041 * used for debugging and performance testing. 00042 **********************************************************************/ 00043 class stop_watch { 00044 #ifdef sun 00045 protected: 00046 hrtime_t _start; 00047 00048 public: 00049 stop_watch() : _start(gethrtime()) {} 00050 00051 // Set stop-watch start time 00052 void set() { _start = gethrtime(); } 00053 00054 // Get stop-watch elapsed time 00055 // XXX - rename -- spell correctly 00056 double elapse_time() const { return (gethrtime()-_start)/1e9; } 00057 00058 // Get current time, in seconds since time orign 00059 static double cur_time() { return gethrtime()/1e9; } 00060 00061 #else 00062 protected: 00063 double _start; 00064 00065 public: 00066 stop_watch() : _start(cur_time()) {} 00067 00068 // Set stop-watch start time 00069 void set() { _start = cur_time(); } 00070 00071 // Get stop-watch elapsed time 00072 double elapse_time() const { return cur_time() - _start; } 00073 00074 // Get current time, in seconds since time orign 00075 static double cur_time() { return the_time(); } 00076 #endif 00077 00078 // prints "msg: X seconds", where X is the elapsed time 00079 void print_time(char* msg="elapsed time") { err_msg("%s: %f seconds", msg, elapse_time()); } 00080 }; 00081 00082 #endif // STOP_WATCH_H_HAS_BEEN_INCLUDED 00083 00084 /* end of file stop_watch.H */