22 #if !defined(_MSC_VER)
34 # pragma warning (disable: 4127)
40 QueryPerformanceFrequency((LARGE_INTEGER *)&t);
41 return 1.0/(t.HighPart*std::pow(2.0, 32) + t.LowPart);
49 QueryPerformanceCounter((LARGE_INTEGER *)&t);
50 return (static_cast<long long>(t.HighPart) << 32) +
51 static_cast<long long>(t.LowPart);
54 gettimeofday(&t, NULL);
55 return static_cast<long long>(t.tv_sec) * 1000000LL +
56 static_cast<long long>(t.tv_usec);
63 #define TIME(expr,esttime) { \
65 long long c1 = r.Count(); \
66 size_t m = int(1.e9/esttime+1); \
68 for (size_t j = m; j; --j) { expr; } \
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); \
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"; \
88 template<
typename Random>
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;
96 volatile double d = 0;
97 std::vector<unsigned long> v;
102 std::cout <<
"Using " << r.Name() <<
" with seed "
103 << r.SeedString() <<
"\n";
105 std::cout <<
"Time system random number generator\n";
106 TIME(i = rand(), 1.0e+01);
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);
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);
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);
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);
132 std::cout <<
"Time returning starting seeds\n";
133 TIME(i = Random::SeedWord(), 1.1e+06);
134 TIME(v = Random::SeedVector(), 1.9e+04);
137 std::cout <<
"Time getting the ready for first random result\n";
138 TIME((r.Reset(), r.SetCount(0)), 6.9e+03);
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);
145 std::cout <<
"Time sampling from a discrete distribution\n";
147 unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 };
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;
153 std::cout <<
"Time shuffling 100 numbers\n";
154 TIME(std::random_shuffle(a.begin(), a.end(), r), 1.3e+03);
157 std::cout <<
"Time with stride = 10\n";
158 TIME(ii = r(), 1.0e+01);
159 TIME(d = nd(r), 8.7e+01);
162 std::cout <<
"Time with stride = 100\n";
163 TIME(ii = r(), 6.8e+01);
164 TIME(d = nd(r), 4.1e+02);
168 if (b && i == 0 && l == 0 && f == 0 && d == 0)
208 catch (
const std::exception& e) {
209 std::cerr <<
"Caught exception: " << e.what() <<
"\n";
213 std::cerr <<
"Caught unknown exception\n";
Header for NormalDistribution.
Generate random integers, reals, and booleans.
Random selection from a discrete set.
Header for Random, RandomGenerator.
RandomCanonical< RandomGenerator > Random
#define TIME(expr, esttime)