Skip to content

Instantly share code, notes, and snippets.

@SteveBronder
Last active June 9, 2020 21:28
Show Gist options
  • Save SteveBronder/4229073f6b12c564d2c0a6bd17cf80f2 to your computer and use it in GitHub Desktop.
Save SteveBronder/4229073f6b12c564d2c0a6bd17cf80f2 to your computer and use it in GitHub Desktop.
--------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------------------------------------------
matrix_value_of_eval_bench/2/manual_time 1498 ns 2820 ns 434421
matrix_value_of_eval_bench/4/manual_time 1969 ns 4593 ns 355822
matrix_value_of_eval_bench/8/manual_time 3877 ns 11459 ns 180711
matrix_value_of_eval_bench/16/manual_time 11431 ns 38782 ns 61078
matrix_value_of_eval_bench/32/manual_time 41160 ns 146687 ns 16991
matrix_value_of_eval_bench/64/manual_time 159694 ns 577776 ns 4382
matrix_value_of_eval_bench/128/manual_time 669791 ns 2344786 ns 1041
matrix_value_of_eval_bench/256/manual_time 2684494 ns 9420535 ns 260
matrix_value_of_eval_bench/512/manual_time 10885500 ns 38044987 ns 64
matrix_value_of_eval_bench/1024/manual_time 43285356 ns 152104199 ns 16
matrix_value_of_eval_bench/2048/manual_time 183239695 ns 652829441 ns 3
matrix_value_of_eval_bench/4096/manual_time 711558553 ns 2703452383 ns 1
matrix_value_of_eval_bench/8192/manual_time 2763816772 ns 10637059080 ns 1
matrix_value_of_bench/2/manual_time 1436 ns 2725 ns 489032
matrix_value_of_bench/4/manual_time 1829 ns 4411 ns 385914
matrix_value_of_bench/8/manual_time 3275 ns 10580 ns 212941
matrix_value_of_bench/16/manual_time 9356 ns 35633 ns 74772
matrix_value_of_bench/32/manual_time 33023 ns 134388 ns 21201
matrix_value_of_bench/64/manual_time 127374 ns 529777 ns 5494
matrix_value_of_bench/128/manual_time 504820 ns 2110577 ns 1386
matrix_value_of_bench/256/manual_time 2024045 ns 8497226 ns 347
matrix_value_of_bench/512/manual_time 8193041 ns 34650847 ns 86
matrix_value_of_bench/1024/manual_time 32740344 ns 137130569 ns 21
matrix_value_of_bench/2048/manual_time 141295428 ns 573438829 ns 5
matrix_value_of_bench/4096/manual_time 583369577 ns 2404615735 ns 1
matrix_value_of_bench/8192/manual_time 2222521730 ns 9482177841 ns 1
vector_value_of_eval_bench/2/manual_time 1305 ns 2318 ns 533826
vector_value_of_eval_bench/4/manual_time 1413 ns 2659 ns 499156
vector_value_of_eval_bench/8/manual_time 1552 ns 3248 ns 451838
vector_value_of_eval_bench/16/manual_time 1862 ns 4405 ns 373855
vector_value_of_eval_bench/32/manual_time 2471 ns 6609 ns 280654
vector_value_of_eval_bench/64/manual_time 3736 ns 11027 ns 188683
vector_value_of_eval_bench/128/manual_time 6245 ns 20036 ns 111776
vector_value_of_eval_bench/256/manual_time 11066 ns 37607 ns 63359
vector_value_of_eval_bench/512/manual_time 20678 ns 72425 ns 33895
vector_value_of_eval_bench/1024/manual_time 39776 ns 141980 ns 17568
vector_value_of_eval_bench/2048/manual_time 78210 ns 282041 ns 8992
vector_value_of_eval_bench/4096/manual_time 155920 ns 561172 ns 4535
vector_value_of_eval_bench/8192/manual_time 310296 ns 1121965 ns 2270
vector_value_of_bench/2/manual_time 1294 ns 2497 ns 541790
vector_value_of_bench/4/manual_time 1386 ns 2846 ns 510221
vector_value_of_bench/8/manual_time 1497 ns 3389 ns 468233
vector_value_of_bench/16/manual_time 1769 ns 4522 ns 399700
vector_value_of_bench/32/manual_time 2267 ns 6678 ns 309377
vector_value_of_bench/64/manual_time 3301 ns 11012 ns 214093
vector_value_of_bench/128/manual_time 5387 ns 19670 ns 130108
vector_value_of_bench/256/manual_time 9455 ns 36770 ns 74033
vector_value_of_bench/512/manual_time 17683 ns 71136 ns 39778
vector_value_of_bench/1024/manual_time 33817 ns 139080 ns 20708
vector_value_of_bench/2048/manual_time 66518 ns 276587 ns 10556
vector_value_of_bench/4096/manual_time 131337 ns 548972 ns 5352
vector_value_of_bench/8192/manual_time 265577 ns 1105918 ns 2648
#include <benchmark/benchmark.h>
#include <stan/math.hpp>
using namespace stan::math;
using namespace Eigen;
using namespace std;
template <typename EigMat>
inline auto value_of_eval(EigMat&& a) {
return a.unaryExpr([](const auto& scal) { return value_of(scal); }).eval();
}
static void vector_value_of_bench(benchmark::State& state) {
const int N = state.range(0);
using var_mat = Matrix<var, -1, 1>;
using dbl_mat = Eigen::Matrix<double, 1, -1>;
for (auto _ : state) {
var_mat a = dbl_mat::Random(N);
var_mat b = dbl_mat::Random(N);
auto start = std::chrono::high_resolution_clock::now();
VectorXd res = value_of(std::move(a)) + value_of(std::move(b));
benchmark::DoNotOptimize(res.data());
benchmark::ClobberMemory();
auto end = std::chrono::high_resolution_clock::now();
recover_memory();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
static void vector_value_of_eval_bench(benchmark::State& state) {
const int N = state.range(0);
using var_mat = Matrix<var, -1, 1>;
using dbl_mat = Matrix<double, -1, 1>;
for (auto _ : state) {
var_mat a = dbl_mat::Random(N);
var_mat b = dbl_mat::Random(N);
auto start = std::chrono::high_resolution_clock::now();
VectorXd res = value_of_eval(std::move(a)) + value_of(std::move(b));
benchmark::DoNotOptimize(res.data());
benchmark::ClobberMemory();
auto end = std::chrono::high_resolution_clock::now();
recover_memory();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
static void matrix_value_of_bench(benchmark::State& state) {
const int N = state.range(0);
using var_mat = Matrix<var, -1, -1>;
using dbl_mat = Matrix<double, -1, -1>;
for (auto _ : state) {
var_mat a = dbl_mat::Random(N, N);
var_mat b = dbl_mat::Random(N, N);
auto start = std::chrono::high_resolution_clock::now();
MatrixXd res = value_of(std::move(a)) + value_of(std::move(b));
benchmark::DoNotOptimize(res.data());
benchmark::ClobberMemory();
auto end = std::chrono::high_resolution_clock::now();
recover_memory();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
static void matrix_value_of_eval_bench(benchmark::State& state) {
const int N = state.range(0);
using var_mat = Matrix<var, -1, -1>;
using dbl_mat = Matrix<double, -1, -1>;
for (auto _ : state) {
var_mat a = dbl_mat::Random(N, N);
var_mat b = dbl_mat::Random(N, N);
auto start = std::chrono::high_resolution_clock::now();
MatrixXd res = value_of_eval(std::move(a)) + value_of(std::move(b));
benchmark::DoNotOptimize(res.data());
benchmark::ClobberMemory();
auto end = std::chrono::high_resolution_clock::now();
recover_memory();
auto elapsed_seconds =
std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
state.SetIterationTime(elapsed_seconds.count());
}
}
int start_val = 2;
int end_val = 8192;
BENCHMARK(matrix_value_of_eval_bench)->RangeMultiplier(2)->Range(start_val, end_val)->UseManualTime();
BENCHMARK(matrix_value_of_bench)->RangeMultiplier(2)->Range(start_val, end_val)->UseManualTime();
BENCHMARK(vector_value_of_eval_bench)->RangeMultiplier(2)->Range(start_val, end_val)->UseManualTime();
BENCHMARK(vector_value_of_bench)->RangeMultiplier(2)->Range(start_val, end_val)->UseManualTime();
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment