00001 #include <iostream>
00002 #include <algorithm>
00003
00004 #include <TRandom2.h>
00005 #include <TStopwatch.h>
00006 #include <TMath.h>
00007
00008 using std::cout;
00009 using std::endl;
00010
00011 const unsigned int NUMTEST = 500;
00012
00013
00014
00015 template <typename T> T randD() {
00016
00017 static TRandom2 r;
00018 return (T) r.Uniform(-500,500);
00019 }
00020
00021 double Time(TStopwatch & w) {
00022
00023 return w.RealTime();
00024 }
00025
00026
00027 template <typename T> double stressVector(unsigned int size, const char* type)
00028 {
00029 cout << "Generating random vector of '"
00030 << type << "' and size " << size << " ..." << endl << endl;
00031
00032 double totalTime = 0;
00033 double totalUnitTime = 0;
00034
00035 T *vector = new T[size];
00036 std::generate(vector, &vector[size], randD<T>);
00037
00038 #ifdef DEBUG
00039 for ( unsigned int i = 0; i < size; ++i )
00040 cout << vector[i] << " " << endl;
00041 #endif
00042
00043 TStopwatch w;
00044 std::cout.precision(6);
00045
00046 unsigned int ntest = 3 * NUMTEST;
00047 w.Start( kTRUE );
00048 for ( unsigned int i = 0; i < ntest; ++i )
00049 TMath::MinElement(size, vector);
00050 w.Stop();
00051 cout << "MinMaxElement() \tTotal Time: " << Time(w) << " (s)\t\t"
00052 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00053 totalUnitTime += Time(w)/ntest;
00054 totalTime += Time(w);
00055
00056 ntest = 3 * NUMTEST;
00057 w.Start( kTRUE );
00058 for ( unsigned int i = 0; i < ntest; ++i )
00059 TMath::LocMin(size, vector);
00060 w.Stop();
00061 cout << "LocMin/Max() \t\tTotal Time: " << Time(w) << " (s)\t\t"
00062 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00063 totalUnitTime += Time(w)/ntest;
00064 totalTime += Time(w);
00065
00066 ntest = 10 * NUMTEST;
00067 w.Start( kTRUE );
00068 for ( unsigned int i = 0; i < ntest; ++i )
00069 TMath::Mean(size, vector);
00070 w.Stop();
00071 cout << "Mean() \t\t\tTotal Time: " << Time(w) << " (s)\t\t"
00072 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00073 totalUnitTime += Time(w)/ntest;
00074 totalTime += Time(w);
00075
00076 ntest = (unsigned int) ( NUMTEST/2.5 );
00077 w.Start( kTRUE );
00078 for ( unsigned int i = 0; i < ntest; ++i )
00079 TMath::Median(size, vector);
00080 w.Stop();
00081 cout << "Median() \t\tTotal Time: " << Time(w) << " (s)\t\t"
00082 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00083 totalUnitTime += Time(w)/ntest;
00084 totalTime += Time(w);
00085
00086 ntest = (unsigned int) ( 10 * NUMTEST );
00087 w.Start( kTRUE );
00088 for ( unsigned int i = 0; i < ntest; ++i )
00089 TMath::RMS(size, vector);
00090 w.Stop();
00091 cout << "RMS() \t\t\tTotal Time: " << Time(w) << " (s)\t\t"
00092 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00093 totalUnitTime += Time(w)/ntest;
00094 totalTime += Time(w);
00095
00096 ntest = (unsigned int) ( NUMTEST/2.5 );
00097 w.Start( kTRUE );
00098 for ( unsigned int i = 0; i < ntest; ++i )
00099 TMath::GeomMean(size, vector);
00100 w.Stop();
00101 cout << "GeomMean() \t\tTotal Time: " << Time(w) << " (s)\t\t"
00102 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00103 totalUnitTime += Time(w)/ntest;
00104 totalTime += Time(w);
00105
00106 UInt_t * index =new UInt_t[size];
00107 ntest = NUMTEST/10;
00108 w.Start( kTRUE );
00109 for ( unsigned int i = 0; i < ntest; ++i )
00110 TMath::Sort(size, vector, index, kFALSE);
00111 w.Stop();
00112 cout << "Sort() \t\t\tTotal Time: " << Time(w) << " (s)\t\t"
00113 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00114 totalUnitTime += Time(w)/ntest;
00115 totalTime += Time(w);
00116
00117 std::sort(vector, vector + size);
00118 #ifdef DEBUG
00119 for ( unsigned int i = 0; i < size; ++i )
00120 cout << vector[i] << " " << endl;
00121 #endif
00122 ntest = 20000*NUMTEST;
00123 w.Start( kTRUE );
00124 for ( unsigned int i = 0; i < ntest; ++i )
00125 TMath::BinarySearch(size, vector, vector[ i % size ]);
00126 w.Stop();
00127 cout << "BinarySearch() \t\tTotal Time: " << Time(w) << " (s)\t\t"
00128 << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << endl;
00129 totalUnitTime += Time(w)/ntest;
00130 totalTime += Time(w);
00131
00132 cout << "\nTotal Time : " << totalTime << " (s)\n"
00133 << "Total Time/call : " << totalUnitTime*1.E3 << " (ms)\n" << endl;
00134
00135 delete [] vector;
00136 delete [] index;
00137
00138 return totalUnitTime;
00139 }
00140
00141 void stressTMath(unsigned int size, const char * type)
00142 {
00143 double totalTime = 0;
00144
00145 cout << "Stress Test Start..." << endl;
00146
00147 if ( strcmp(type, "Short_t") == 0 )
00148 totalTime += stressVector<Short_t>(size, type);
00149 else if ( strcmp(type, "Int_t") == 0 )
00150 totalTime += stressVector<Int_t>(size, type);
00151 else if ( strcmp(type, "Float_t") == 0 )
00152 totalTime += stressVector<Float_t>(size, type);
00153 else if ( strcmp(type, "Long_t") == 0 )
00154 totalTime += stressVector<Long_t>(size, type);
00155 else if ( strcmp(type, "Long64_t") == 0 )
00156 totalTime += stressVector<Long64_t>(size, type);
00157 else
00158 totalTime += stressVector<Double_t>(size, "Double_t");
00159
00160
00161
00162 cout << "End of Stress Test..." << endl;
00163
00164 return;
00165 }
00166
00167
00168 int main(int argc, char* argv[])
00169 {
00170
00171 unsigned int size = 100000;
00172 const char * type = "Double_t";
00173
00174 if ( argc > 1 ) {
00175 if (strcmp(argv[1], "-h") == 0) {
00176 cout << "Usage: " << argv[0]
00177 << " [TYPE OF ARRAY] [SIZE OF ARRAY]\n\n"
00178 << "where [TYPE OF ARRAY] is one of the following:\n"
00179 << "\t\"Short_t\"\n"
00180 << "\t\"Int_t\"\n"
00181 << "\t\"Float_t\"\n"
00182 << "\t\"Long_t\"\n"
00183 << "\t\"Long64_t\"\n"
00184 << "\t \"Double_t\"\n"
00185 << endl;
00186 return 1;
00187 }
00188 type = argv[1];
00189 }
00190
00191
00192 if ( argc > 2 )
00193 size = (unsigned int) atoi(argv[2]);
00194
00195 stressTMath(size, type);
00196
00197 return 0;
00198 }