Skip to content

Instantly share code, notes, and snippets.

@motoishmz
Last active February 4, 2017 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motoishmz/1e81167a22d5a78a1d021d25a63b3d1b to your computer and use it in GitHub Desktop.
Save motoishmz/1e81167a22d5a78a1d021d25a63b3d1b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <random>
#include <chrono>
#include <functional>
#include <string>
using namespace std;
std::mt19937 random_engine;
std::uniform_real_distribution<float> random_distribution;
float of_random(float max) {
// copied from ofRandom()
return (max * rand() / RAND_MAX) * (1.0f - std::numeric_limits<float>::epsilon());
}
float my_random(float max) {
return random_distribution(random_engine) * max;
}
namespace FastRandom
{
static unsigned long xorshf96(void) {
static unsigned long x = 123456789, y = 362436069, z = 521288629;
unsigned long t;
x ^= x << 16;
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y;
y = z;
z = t ^ x ^ y;
return z;
}
static inline unsigned long random(void) {
return xorshf96();
}
static inline float randomf(void) {
return (float)random() / (float)std::numeric_limits<unsigned long>::max();
}
};
double my_stopwatch(const std::string label, std::function<void()> f) {
cout << " ---- " << endl;
cout << "[ " << label << " ]" << endl;
auto start = chrono::steady_clock::now();
f();
auto end = chrono::steady_clock::now();
auto record = chrono::duration<double, micro>(end - start).count();
cout << "record:" << record * 0.001 << " ms." << endl;
return record;
}
int main(int argc, char *argv[]) {
const int num_loop = 50000;
random_distribution.param(std::uniform_real_distribution<float>::param_type(0.0, 1.0));
random_engine.seed(std::random_device()());
{
const auto record = my_stopwatch("ofRandom", [=]() {
for (int i = 0; i < num_loop; i++) {
auto r = of_random(3.0f);
}
});
cout << "average:" << record / num_loop << " micro seconds." << endl;
}
{
const auto record = my_stopwatch("my_random", [=]() {
for (int i = 0; i < num_loop; i++) {
auto r = my_random(3.0f);
}
});
cout << "average:" << record / num_loop << " micro seconds." << endl;
}
{
const auto record = my_stopwatch("FastRandom::randomf", [=]() {
for (int i = 0; i < num_loop; i++) {
auto r = FastRandom::randomf() * 3.0f;
}
});
cout << "average:" << record / num_loop << " micro seconds." << endl;
}
return 0;
}
@motoishmz
Copy link
Author

motoishmz commented Feb 4, 2017

ElCapitan OF0.9.7 結果だいたいこんな感じ

[ ofRandom ]
record:0.297501 ms.
average:0.00595002 micro seconds.

[ my_random ]
record:1.72936 ms.
average:0.0345872 micro seconds.

[ FastRandom::randomf ]
record:0.763401 ms.
average:0.015268 micro seconds.

@motoishmz
Copy link
Author

Windows 10 OF0.9.7での結果

[ ofRandom ]
record:3.94065 ms.
average:0.078813 micro seconds.

[ my_random ]
record:5.50069 ms.
average:0.110014 micro seconds.

[ FastRandom::randomf ]
record:0.844018 ms.
average:0.0168804 micro seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment