00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <iostream>
00026 #include <iomanip>
00027
00028 #include <Basics.H>
00029 #include "Timer.H"
00030
00031 using std::ios;
00032 using std::cout;
00033 using std::cerr;
00034 using std::endl;
00035 using std::setprecision;
00036 using std::setiosflags;
00037
00038 const int NumPasses = 1000;
00039 const int InnerLoopCnt = 15000;
00040
00041 void Error(char *msg) throw ()
00042 {
00043 cerr << "Error: " << msg << '\n';
00044 cerr << "Exiting...\n";
00045 exit(1);
00046 }
00047
00048 int main()
00049 {
00050 Timer::T timer;
00051
00052
00053 if (timer.Elapsed() != 0UL) {
00054 Error("default timer not initialized with 0 elapsed time");
00055 }
00056
00057
00058 cout << "Timer granularity = " << Timer::Grain << " usecs\n";
00059
00060
00061 Timer::MicroSecs total = 0UL, t, maxT, minT;
00062 int nonZeroCnt = 0;
00063 int i;
00064 for (i = 0; i < NumPasses; i++) {
00065 timer.Start();
00066 t = timer.Stop();
00067 if (t != 0UL) {
00068 nonZeroCnt++;
00069 }
00070 total += t;
00071 if (i == 0) {
00072 maxT = minT = t;
00073 } else {
00074 maxT = max(maxT, t);
00075 minT = min(minT, t);
00076 }
00077 }
00078 if (total != timer.Elapsed()) {
00079 Error("total elapsed time not recorded properly");
00080 }
00081 cout << "Average time for empty run = "
00082 << setprecision(1) << setiosflags(ios::fixed)
00083 << ((float)total/(float)NumPasses) << " usecs";
00084 cout << "; min/max = " << minT << '/' << maxT << " usecs\n";
00085
00086
00087 Timer::MicroSecs elapsed = timer.Reset();
00088 if (elapsed != total) {
00089 Error("incorrect value returned by 'Reset'");
00090 }
00091 if (timer.Elapsed() != 0UL) {
00092 Error("'Reset' failed");
00093 }
00094 total = 0UL;
00095
00096
00097 for (i = 0; i < NumPasses; i++) {
00098 timer.Start();
00099 for (int j = 0; j < InnerLoopCnt; j++) {
00100 int k = j / 2; k *= 3;
00101 }
00102 t = timer.Stop();
00103 total += t;
00104 if (i == 0) {
00105 maxT = minT = t;
00106 } else {
00107 maxT = max(maxT, t);
00108 minT = min(minT, t);
00109 }
00110 }
00111 if (total != timer.Elapsed()) {
00112 Error("total elapsed time not recorded properly");
00113 }
00114 cout << "Average time for non-empty run = "
00115 << setprecision(1) << setiosflags(ios::fixed)
00116 << ((float)total/(float)NumPasses) << " usecs";
00117 cout << "; min/max = " << minT << '/' << maxT << " usecs\n";
00118
00119 cout << "All tests passed!\n";
00120 return 0;
00121 }