Skip to content

Instantly share code, notes, and snippets.

@MikeLing
Last active June 27, 2017 14:20
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 MikeLing/d258cf0c7e18d17639c577a695dbec41 to your computer and use it in GitHub Desktop.
Save MikeLing/d258cf0c7e18d17639c577a695dbec41 to your computer and use it in GitHub Desktop.
#include <shogun/base/init.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/Math.h>
#include <random>
#include <benchmark/benchmark.h>
using namespace shogun;
using namespace std;
static void BM_mt19937_float64(benchmark::State& state)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(1, 2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_mt19937_64_float64(benchmark::State& state)
{
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_real_distribution<> dis(1, 2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_minstd_rand_float64(benchmark::State& state)
{
std::random_device rd;
std::minstd_rand gen(rd());
std::uniform_real_distribution<> dis(1, 2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_minstd_rand0_float64(benchmark::State& state)
{
std::random_device rd;
std::minstd_rand0 gen(rd());
std::uniform_real_distribution<> dis(1, 2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_ranlux24_float64(benchmark::State& state)
{
std::random_device rd;
std::ranlux24_base gen(rd());
std::uniform_real_distribution<> dis(1, 2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_ranlux48_float64(benchmark::State& state)
{
std::random_device rd;
std::ranlux48_base gen(rd());
std::uniform_real_distribution<> dis(1, 2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_sg_random_float64(benchmark::State& state)
{
shogun::init_shogun();
shogun::CRandom* rand = new shogun::CRandom();
while (state.KeepRunning())
{
rand->random(1.0, 2.0);
}
}
static void BM_mt19937_int32(benchmark::State& state)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 6);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_mt19937_64_int32(benchmark::State& state)
{
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<> dis(1, 6);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_minstd_rand_int32(benchmark::State& state)
{
std::random_device rd;
std::minstd_rand gen(rd());
std::uniform_int_distribution<> dis(1, 6);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_minstd_rand0_int32(benchmark::State& state)
{
std::random_device rd;
std::minstd_rand0 gen(rd());
std::uniform_int_distribution<> dis(1, 6);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_ranlux24_int32(benchmark::State& state)
{
std::random_device rd;
std::ranlux24_base gen(rd());
std::uniform_int_distribution<> dis(1, 6);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_ranlux48_int32(benchmark::State& state)
{
std::random_device rd;
std::ranlux48_base gen(rd());
std::uniform_int_distribution<> dis(1, 6);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_sg_random_int32(benchmark::State& state)
{
shogun::init_shogun();
shogun::CRandom* rand = new shogun::CRandom();
while (state.KeepRunning())
{
rand->random(1, 6);
}
}
static void BM_mt19937_normal_dis(benchmark::State& state)
{
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<> dis(5,2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_mt19937_64_normal_dis(benchmark::State& state)
{
std::random_device rd;
std::mt19937_64 gen(rd());
std::normal_distribution<> dis(5,2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_minstd_rand_normal_dis(benchmark::State& state)
{
std::random_device rd;
std::minstd_rand gen(rd());
std::normal_distribution<> dis(5,2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_minstd_rand0_normal_dis(benchmark::State& state)
{
std::random_device rd;
std::minstd_rand0 gen(rd());
std::normal_distribution<> dis(5,2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_ranlux24_normal_dis(benchmark::State& state)
{
std::random_device rd;
std::ranlux24_base gen(rd());
std::normal_distribution<> dis(5,2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_ranlux48_normal_dis(benchmark::State& state)
{
std::random_device rd;
std::ranlux48_base gen(rd());
std::normal_distribution<> dis(5,2);
while (state.KeepRunning())
{
dis(gen);
}
}
static void BM_sg_random_normal_dis(benchmark::State& state)
{
shogun::init_shogun();
shogun::CRandom* rand = new shogun::CRandom();
while (state.KeepRunning())
{
rand->normal_distrib(5, 2);
}
}
// Register the function as a benchmark
BENCHMARK(BM_sg_random_float64);
BENCHMARK(BM_mt19937_float64);
BENCHMARK(BM_mt19937_64_float64);
BENCHMARK(BM_minstd_rand_float64);
BENCHMARK(BM_minstd_rand0_float64);
BENCHMARK(BM_ranlux24_float64);
BENCHMARK(BM_ranlux48_float64);
BENCHMARK(BM_sg_random_int32);
BENCHMARK(BM_mt19937_int32);
BENCHMARK(BM_mt19937_64_int32);
BENCHMARK(BM_minstd_rand_int32);
BENCHMARK(BM_minstd_rand0_int32);
BENCHMARK(BM_ranlux24_int32);
BENCHMARK(BM_ranlux48_int32);
BENCHMARK(BM_sg_random_normal_dis);
BENCHMARK(BM_mt19937_normal_dis);
BENCHMARK(BM_mt19937_64_normal_dis);
BENCHMARK(BM_minstd_rand_normal_dis);
BENCHMARK(BM_minstd_rand0_normal_dis);
BENCHMARK(BM_ranlux24_normal_dis);
BENCHMARK(BM_ranlux48_normal_dis);
BENCHMARK_MAIN();
@MikeLing
Copy link
Author

MikeLing commented Jun 20, 2017

Here is the from clang result:

screen shot 2017-06-27 at 10 18 04 pm

@MikeLing
Copy link
Author

MikeLing commented Jun 20, 2017

Here is the result from gcc:
screen shot 2017-06-27 at 10 17 46 pm

@MikeLing
Copy link
Author

MikeLing commented Jun 21, 2017

Benchmark(float64) Time CPU Iterations
BM_mt19937_float64 166 ns 97 ns 7086024
BM_mt19937_64_float64 42 ns 41 ns 15468554
BM_minstd_rand_float64 31 ns 30 ns 22481581
BM_minstd_rand0_float64 36 ns 35 ns 21570851
BM_ranlux24_float64 107 ns 100 ns 5723537
BM_ranlux48_float64 75 ns 71 ns 9106046
BM_sg_random_float64 35 ns 35 ns 20147480
Benchmark(int32) Time CPU Iterations
BM_mt19937_int32 113 ns 112 ns 5895581
BM_mt19937_64_int32 116 ns 115 ns 6188830
BM_minstd_rand_int32 123 ns 120 ns 6015710
BM_minstd_rand0_int32 127 ns 124 ns 6006934
BM_ranlux24_int32 135 ns 134 ns 5380311
BM_ranlux48_int32 133 ns 132 ns 5376922
BM_sg_random_int32 38 ns 38 ns 9625168

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