RandomLib  1.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
RandomTime.cpp
Go to the documentation of this file.
1 /**
2  * \file RandomTime.cpp
3  * \brief Timing %RandomLib
4  *
5  * Compile/link with, e.g.,\n
6  * g++ -I../include -O2 -funroll-loops
7  * -o RandomTime RandomTime.cpp ../src/Random.cpp\n
8  * ./RandomTime
9  *
10  * See \ref timing, for more information.
11  *
12  * Copyright (c) Charles Karney (2006-2011) <charles@karney.com> and licensed
13  * under the MIT/X11 License. For more information, see
14  * http://randomlib.sourceforge.net/
15  **********************************************************************/
16 
17 #include <iostream>
18 #include <iomanip>
19 #include <vector>
20 #include <string>
21 #include <ctime>
22 #if !defined(_MSC_VER)
23 #include <sys/time.h>
24 #else
25 #include <windows.h>
26 #include <winbase.h>
27 #endif
28 #include <RandomLib/Random.hpp>
31 
32 #if defined(_MSC_VER)
33 // Squelch warnings about constant conditional expressions
34 # pragma warning (disable: 4127)
35 #endif
36 
37 double HighPrecMult() {
38 #if defined(_MSC_VER)
39  LARGE_INTEGER t;
40  QueryPerformanceFrequency((LARGE_INTEGER *)&t);
41  return 1.0/(t.HighPart*std::pow(2.0, 32) + t.LowPart);
42 #else
43  return 1.e-6;
44 #endif
45 }
46 long long HighPrecTime() {
47 #if defined(_MSC_VER)
48  LARGE_INTEGER t;
49  QueryPerformanceCounter((LARGE_INTEGER *)&t);
50  return (static_cast<long long>(t.HighPart) << 32) +
51  static_cast<long long>(t.LowPart);
52 #else
53  timeval t;
54  gettimeofday(&t, NULL);
55  return static_cast<long long>(t.tv_sec) * 1000000LL +
56  static_cast<long long>(t.tv_usec);
57 #endif
58 }
59 
60 // estime is the estimated time for the command in ns. The command is executed
61 // as many time as necessary to fill a second.
62 
63 #define TIME(expr,esttime) { \
64  long long t1, t2; \
65  long long c1 = r.Count(); \
66  size_t m = int(1.e9/esttime+1); \
67  t1=HighPrecTime(); \
68  for (size_t j = m; j; --j) { expr; } \
69  t2=HighPrecTime(); \
70  std::cout << std::setprecision(1) << std::setw(8) << std::scientific \
71  << 0.1*std::floor((t2-t1)*HighPrecMult()*1.0e10/m+0.5) << "ns "; \
72  std::string cmd(#expr); \
73  std::string::size_type p; \
74  p = cmd.find("template "); \
75  if (p != std::string::npos) cmd = cmd.substr(0,p) + cmd.substr(p+9); \
76  p = cmd.find(" = "); \
77  if (p != std::string::npos) cmd = cmd.substr(p + 3); \
78  p = cmd.find("Random::"); \
79  if (p != std::string::npos) cmd = cmd.substr(0,p)+cmd.substr(p+8); \
80  p = cmd.find("std::"); \
81  if (p != std::string::npos) cmd = cmd.substr(0,p)+cmd.substr(p+5); \
82  if (cmd[0] == '(') \
83  cmd = cmd.substr(1,cmd.size()-2); \
84  std::cout << std::setprecision(1) << std::setw(5) << std::fixed \
85  << (r.Count()-c1)/float(m) << "rv" << " per " << cmd << "\n"; \
86  }
87 
88 template<typename Random>
89 void Time(Random& r) {
90 
91  volatile bool b = false;
92  volatile unsigned i = 0, n = 0;
93  volatile typename Random::result_type ii = 0;
94  volatile unsigned long long l = 0;
95  volatile float f = 0;
96  volatile double d = 0;
97  std::vector<unsigned long> v;
98 
99  ii = r();
100  if (ii == 0) n = 1;
101 
102  std::cout << "Using " << r.Name() << " with seed "
103  << r.SeedString() << "\n";
104 
105  std::cout << "Time system random number generator\n";
106  TIME(i = rand(), 1.0e+01);
107 
108  std::cout << "Time generation of integer results\n";
109  TIME(ii = r(), 2.0e+00);
110  TIME(i = r.template Integer<unsigned>(), 2.6e+00);
111  TIME(l = r.template Integer<unsigned long long>(), 4.3e+00);
112  TIME(i = (r.template Integer<unsigned,6>()), 2.6e+00);
113  TIME(i = r.template Integer<unsigned>(52u), 5.6e+00);
114  TIME(i = r.template Integer<unsigned>(52u+n), 1.3e+01);
115 
116  std::cout << "Time generation of real results\n";
117  TIME(f = r.template Fixed<float>(), 4.9e+00);
118  TIME(d = r.template Fixed<double>(), 9.5e+00);
119  TIME(f = r.template Float<float>(), 1.9e+01);
120  TIME(d = r.template Float<double>(), 1.8e+01);
121 
122  std::cout << "Time generation of boolean results\n";
123  TIME(b = r.template Prob<float>(0.28f), 1.1e+01);
124  TIME(b = r.template Prob<double>(0.28), 7.7e+00);
125 
126  std::cout << "Time generation of normal distribution\n";
129  TIME(f = nf(r), 4.1e+01);
130  TIME(d = nd(r), 5.4e+01);
131 
132  std::cout << "Time returning starting seeds\n";
133  TIME(i = Random::SeedWord(), 1.1e+06);
134  TIME(v = Random::SeedVector(), 1.9e+04);
135 
136  r.Reset();
137  std::cout << "Time getting the ready for first random result\n";
138  TIME((r.Reset(), r.SetCount(0)), 6.9e+03);
139 
140  r.SetCount(123);
141  std::cout << "Time stepping the generator forward and back\n";
142  TIME(r.StepCount(10000), 6.3e+03);
143  TIME(r.StepCount(-10000), 1.1e+04);
144 
145  std::cout << "Time sampling from a discrete distribution\n";
146  // Weights for throwing a pair of dice
147  unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
148  RandomLib::RandomSelect<float> seld(w, w+13);
149  TIME(i = seld(r), 2.8e+01);
150  std::vector<int> a(101);
151  for (int m = 0; m < 101; ++m) a[m] = m;
152 
153  std::cout << "Time shuffling 100 numbers\n";
154  TIME(std::random_shuffle(a.begin(), a.end(), r), 1.3e+03);
155 
156  r.SetStride(10);
157  std::cout << "Time with stride = 10\n";
158  TIME(ii = r(), 1.0e+01);
159  TIME(d = nd(r), 8.7e+01);
160 
161  r.SetStride(100);
162  std::cout << "Time with stride = 100\n";
163  TIME(ii = r(), 6.8e+01);
164  TIME(d = nd(r), 4.1e+02);
165 
166  r.SetStride();
167  // Avoid warning about set but unused variables
168  if (b && i == 0 && l == 0 && f == 0 && d == 0)
169  r.StepCount(0);
170  return;
171 }
172 
173 int main(int, char**) {
174  try {
175  {
176  typedef RandomLib::SRandom32 R;
177  R::SelfTest();
178  R r;
179  r.StepCount(123);
180  Time<R>(r);
181  }
182  {
183  typedef RandomLib::SRandom64 R;
184  R::SelfTest();
185  R r;
186  r.StepCount(123);
187  Time<R>(r);
188  }
189  if (false) {
190  // Skip timing MRandom{32,64}
191  {
192  typedef RandomLib::MRandom32 R;
193  R::SelfTest();
194  R r;
195  r.StepCount(123);
196  Time<R>(r);
197  }
198  {
199  typedef RandomLib::MRandom64 R;
200  R::SelfTest();
201  R r;
202  r.StepCount(123);
203  Time<R>(r);
204  }
205  }
206  return 0;
207  }
208  catch (const std::exception& e) {
209  std::cerr << "Caught exception: " << e.what() << "\n";
210  return 1;
211  }
212  catch (...) {
213  std::cerr << "Caught unknown exception\n";
214  return 1;
215  }
216 }
Header for NormalDistribution.
Header for RandomSelect.
Generate random integers, reals, and booleans.
Random selection from a discrete set.
Header for Random, RandomGenerator.
void Time(Random &r)
Definition: RandomTime.cpp:89
int main(int, char **)
Definition: RandomTime.cpp:173
double HighPrecMult()
Definition: RandomTime.cpp:37
RandomCanonical< RandomGenerator > Random
Definition: Random.hpp:135
#define TIME(expr, esttime)
Definition: RandomTime.cpp:63
long long HighPrecTime()
Definition: RandomTime.cpp:46