ROOT  6.06/09
Reference Guide
stressTMath.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <algorithm>
3 
4 #include <TRandom2.h>
5 #include <TStopwatch.h>
6 #include <TMath.h>
7 
8 using std::cout;
9 using std::endl;
10 
11 const unsigned int NUMTEST = 500;
12 
13 // #define DEBUG
14 
15 template <typename T> T randD() {
16  // use default seed to get same sequence
17  static TRandom2 r;
18  return (T) r.Uniform(-500,500);
19 }
20 
21 double Time(TStopwatch & w) {
22  //return w.CpuTime();
23  return w.RealTime();
24 }
25 
26 
27 template <typename T> double stressVector(unsigned int size, const char* type)
28 {
29  std::cout << "Generating random vector of '"
30  << type << "' and size " << size << " ..." << std::endl << std::endl;
31 
32  double totalTime = 0;
33  double totalUnitTime = 0;
34 
35  T *vector = new T[size];
36  std::generate(vector, &vector[size], randD<T>);
37 
38 #ifdef DEBUG
39  for ( unsigned int i = 0; i < size; ++i )
40  std::cout << vector[i] << " " << std::endl;
41 #endif
42 
43  TStopwatch w;
44  std::cout.precision(6);
45 
46  unsigned int ntest = 3 * NUMTEST;
47  w.Start( kTRUE );
48  volatile int iresult = 0;
49  for ( unsigned int i = 0; i < ntest; ++i )
50  iresult = TMath::MinElement(size, vector);
51  w.Stop();
52  std::cout << "MinMaxElement() \tTotal Time: " << Time(w) << " (s)\t\t"
53  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
54  totalUnitTime += Time(w)/ntest;
55  totalTime += Time(w);
56 
57  ntest = 3 * NUMTEST;
58  w.Start( kTRUE );
59  for ( unsigned int i = 0; i < ntest; ++i )
60  iresult = TMath::LocMin(size, vector);
61  w.Stop();
62  std::cout << "LocMin/Max() \t\tTotal Time: " << Time(w) << " (s)\t\t"
63  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
64  totalUnitTime += Time(w)/ntest;
65  totalTime += Time(w);
66 
67  ntest = 10 * NUMTEST;
68  w.Start( kTRUE );
69  volatile double result;
70  for ( unsigned int i = 0; i < ntest; ++i )
71  result = TMath::Mean(size, vector);
72  w.Stop();
73  std::cout << "Mean() \t\t\tTotal Time: " << Time(w) << " (s)\t\t"
74  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
75  totalUnitTime += Time(w)/ntest;
76  totalTime += Time(w);
77 
78  ntest = (unsigned int) ( NUMTEST/2.5 );
79  w.Start( kTRUE );
80  for ( unsigned int i = 0; i < ntest; ++i )
81  result = TMath::Median(size, vector);
82  w.Stop();
83  std::cout << "Median() \t\tTotal Time: " << Time(w) << " (s)\t\t"
84  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
85  totalUnitTime += Time(w)/ntest;
86  totalTime += Time(w);
87 
88  ntest = (unsigned int) ( 10 * NUMTEST );
89  w.Start( kTRUE );
90  for ( unsigned int i = 0; i < ntest; ++i )
91  result = TMath::RMS(size, vector);
92  w.Stop();
93  std::cout << "RMS() \t\t\tTotal Time: " << Time(w) << " (s)\t\t"
94  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
95  totalUnitTime += Time(w)/ntest;
96  totalTime += Time(w);
97 
98  ntest = (unsigned int) ( NUMTEST/2.5 );
99  w.Start( kTRUE );
100  for ( unsigned int i = 0; i < ntest; ++i )
101  result = TMath::GeomMean(size, vector);
102  w.Stop();
103  std::cout << "GeomMean() \t\tTotal Time: " << Time(w) << " (s)\t\t"
104  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
105  totalUnitTime += Time(w)/ntest;
106  totalTime += Time(w);
107 
108  UInt_t * index =new UInt_t[size];
109  ntest = NUMTEST/10;
110  w.Start( kTRUE );
111  for ( unsigned int i = 0; i < ntest; ++i )
112  TMath::Sort(size, vector, index, kFALSE);
113  w.Stop();
114  std::cout << "Sort() \t\t\tTotal Time: " << Time(w) << " (s)\t\t"
115  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
116  totalUnitTime += Time(w)/ntest;
117  totalTime += Time(w);
118 
119  std::sort(vector, vector + size);
120 #ifdef DEBUG
121  for ( unsigned int i = 0; i < size; ++i )
122  std::cout << vector[i] << " " << std::endl;
123 #endif
124  ntest = 20000*NUMTEST;
125  w.Start( kTRUE );
126  for ( unsigned int i = 0; i < ntest; ++i )
127  iresult = TMath::BinarySearch(size, vector, vector[ i % size ]);
128  w.Stop();
129  std::cout << "BinarySearch() \t\tTotal Time: " << Time(w) << " (s)\t\t"
130  << " Time/call: " << Time(w)/(ntest)*1.E6 << " (microsec)" << std::endl;
131  totalUnitTime += Time(w)/ntest;
132  totalTime += Time(w);
133 
134  std::cout << "\nTotal Time : " << totalTime << " (s)\n"
135  << "Total Time/call : " << totalUnitTime*1.E3 << " (ms)\n" << std::endl;
136 
137  // to avoid the warning for un-used variables
138  (void)result;
139  (void)iresult;
140 
141  delete [] vector;
142  delete [] index;
143 
144  return totalUnitTime;
145 }
146 
147 void stressTMath(unsigned int size, const char * type)
148 {
149  double totalTime = 0;
150 
151  std::cout << "Stress Test Start..." << std::endl;
152 
153  if ( strcmp(type, "Short_t") == 0 )
154  totalTime += stressVector<Short_t>(size, type);
155  else if ( strcmp(type, "Int_t") == 0 )
156  totalTime += stressVector<Int_t>(size, type);
157  else if ( strcmp(type, "Float_t") == 0 )
158  totalTime += stressVector<Float_t>(size, type);
159  else if ( strcmp(type, "Long_t") == 0 )
160  totalTime += stressVector<Long_t>(size, type);
161  else if ( strcmp(type, "Long64_t") == 0 )
162  totalTime += stressVector<Long64_t>(size, type);
163  else
164  totalTime += stressVector<Double_t>(size, "Double_t");
165 
166  //std::cout << "Total Test Time: " << totalTime << "\n" << std::endl;
167 
168  std::cout << "End of Stress Test..." << std::endl;
169 
170  return;
171 }
172 
173 
174 int main(int argc, char* argv[])
175 {
176  // Default size and data type
177  unsigned int size = 100000;
178  const char * type = "Double_t";
179 
180  if ( argc > 1 ) {
181  if (strcmp(argv[1], "-h") == 0) {
182  std::cout << "Usage: " << argv[0]
183  << " [TYPE OF ARRAY] [SIZE OF ARRAY]\n\n"
184  << "where [TYPE OF ARRAY] is one of the following:\n"
185  << "\t\"Short_t\"\n"
186  << "\t\"Int_t\"\n"
187  << "\t\"Float_t\"\n"
188  << "\t\"Long_t\"\n"
189  << "\t\"Long64_t\"\n"
190  << "\t \"Double_t\"\n"
191  << std::endl;
192  return 1;
193  }
194  type = argv[1];
195  }
196 
197 
198  if ( argc > 2 )
199  size = (unsigned int) atoi(argv[2]);
200 
201  stressTMath(size, type);
202 
203  return 0;
204 }
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
double T(double x)
Definition: ChebyshevPol.h:34
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:29
T randD()
Definition: stressTMath.cxx:15
const Bool_t kFALSE
Definition: Rtypes.h:92
void generate(R &r, TH1D *h)
Definition: piRandom.C:28
Double_t GeomMean(Long64_t n, const T *a)
Definition: TMath.h:855
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:916
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
double stressVector(unsigned int size, const char *type)
Definition: stressTMath.cxx:27
void stressTMath(unsigned int size, const char *type)
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Definition: TMath.h:1002
ROOT::R::TRInterface & r
Definition: Object.C:4
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:824
unsigned int UInt_t
Definition: RtypesCore.h:42
int type
Definition: TGX11.cxx:120
Double_t Median(Long64_t n, const T *a, const Double_t *w=0, Long64_t *work=0)
Definition: TMath.h:1077
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
int main(int argc, char *argv[])
double Time(TStopwatch &w)
Definition: stressTMath.cxx:21
typedef void((*Func_t)())
const unsigned int NUMTEST
Definition: stressTMath.cxx:11
double result[121]
Long64_t LocMin(Long64_t n, const T *a)
Definition: TMath.h:695
const Bool_t kTRUE
Definition: Rtypes.h:91
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMath.h:944
T MinElement(Long64_t n, const T *a)
Definition: TMath.h:681
Stopwatch class.
Definition: TStopwatch.h:30