00001 /*
00002 File: Timer.cc
00003
00004 Function: See header file
00005
00006 Author(s): Andrew Willmott
00007
00008 Copyright: (c) 1995-2000, Andrew Willmott
00009
00010 Notes: Any other useful timing calls?
00011 */
00012
00013
00014 #include "cl/Timer.h"
00015 #include <stdio.h>
00016 #include <math.h>
00017
00018 Void Timer::StartTimer()
00019 {
00020 startTime = CurrentTime();
00021 lapTime = startTime;
00022 }
00023
00024 Float Timer::GetTimer()
00025 {
00026 return(CurrentTime() - startTime);
00027 }
00028
00029 Float Timer::DeltaTime()
00030 {
00031 Float oldLapTime = lapTime;
00032
00033 lapTime = CurrentTime();
00034
00035 return(lapTime - oldLapTime);
00036 }
00037
00038 Void Timer::StopTimer() /* Stops timer */
00039 {
00040 stopTime = CurrentTime();
00041 }
00042
00043 Void Timer::ContTimer() /* Restarts timer */
00044 {
00045 Float temp;
00046
00047 temp = CurrentTime();
00048 temp -= stopTime;
00049 startTime += temp;
00050 lapTime += temp;
00051 }
00052
00053 /* --- Unix Time ----------------------------------------------------- */
00054
00055
00056 #ifdef UNIX_TIME
00057
00058 /* Use the times() call */
00059
00060 #include <unistd.h>
00061 #include <sys/times.h>
00062 #include <sys/time.h>
00063 #include <errno.h>
00064 #include "cl/String.h"
00065
00066 Float ProgramTimer::CurrentTime()
00067 {
00068 struct tms tb;
00069 Float result;
00070
00071 times(&tb);
00072 result = (Float)(tb.tms_utime) / CLK_TCK;
00073 if (addSystem)
00074 result += (Float)(tb.tms_stime) / CLK_TCK;
00075
00076 return(result);
00077 }
00078
00079 Float WallClockTimer::CurrentTime()
00080 {
00081 struct timeval tv;
00082 Float result;
00083
00084 if (gettimeofday(&tv, 0))
00085 _Error(String().Printf("gettimeofday call failed: %d", errno));
00086
00087 result = (tv.tv_sec & 0x0FFFF) + tv.tv_usec * 1e-6;
00088
00089 return(result);
00090 }
00091
00092 #endif
00093
00094
00095 /* --- Ansi Time ------------------------------------------------------ */
00096
00097
00098 #ifdef ANSI_TIME /* Use clock() */
00099
00100 #include <time.h>
00101
00102 Float WallClockTimer::CurrentTime()
00103 {
00104 return(clock() / ((Float) CLOCKS_PER_SEC));
00105 }
00106
00107 #endif
00108
00109
00110 /* --- Sgi Time ------------------------------------------------------ */
00111
00112
00113 #ifdef SGI_TIME
00114
00115 // uses SGI multimedia routines.
00116
00117 #include <dmedia/dmedia.h>
00118
00119 typedef unsigned long long UST;
00120
00121 Float WallClockTimer::CurrentTime()
00122 {
00123 UST temp;
00124
00125 dmGetUST(&temp);
00126
00127 return(((Float) temp) / 1e9);
00128 }
00129
00130 #endif
00131
00132
00133 // --- Use rusage call ---------------------------------------------------------
00134
00135
00136 #ifdef RUSAGE_TIME
00137
00138 #include <sys/time.h>
00139 #include <sys/resource.h>
00140 #include <unistd.h>
00141
00142 Float ProgramTimer::CurrentTime()
00143 {
00144 struct rusage usage;
00145 Float result;
00146
00147 getrusage(RUSAGE_SELF, &usage);
00148
00149 result = (usage.ru_utime.tv_sec & 0x0FFFF) + usage.ru_utime.tv_usec * 1e-6;
00150 if (addSystem)
00151 result += (usage.ru_stime.tv_sec & 0x0FFFF) + usage.ru_stime.tv_usec * 1e-6;
00152
00153 return(result);
00154 }
00155
00156 #endif