Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestTimer.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Thu Aug  5 10:18:06 EDT 2004 by ken@xorian.net  
00020 //      modified on Sat Feb 12 17:31:31 PST 2000 by mann  
00021 //      modified on Fri Jul 19 09:38:16 PDT 1996 by heydon
00022 
00023 // Program to test the "Timer.H" interface
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     // verify default timer
00053     if (timer.Elapsed() != 0UL) {
00054         Error("default timer not initialized with 0 elapsed time");
00055     }
00056 
00057     // print granularity
00058     cout << "Timer granularity = " << Timer::Grain << " usecs\n";
00059 
00060     // try many empty timer runs
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++; // set breakpoint here
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     // test "Reset"
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     // try many non-empty timer runs
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 }

Generated on Mon May 8 00:48:33 2006 for Vesta by  doxygen 1.4.2