1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
// Copyright 2017 Global Phasing Ltd.
// Microbenchmark of gemmi::SMat33::r_u_r().
// Requires the google/benchmark library. It can be built manually:
// c++ -Wall -O2 -I../include -I$GB/include smat33.cpp $GB/src/libbenchmark.a -pthread
#include "gemmi/math.hpp"
#include <benchmark/benchmark.h>
#include <stdlib.h> // for rand
static void r_u_r1(benchmark::State& state) {
gemmi::Vec3 rr[10] = {
{-1., -2., -3.},
{ 1., 1., -5.},
{-1., 6., 3.},
{ 5., -2., 9.},
{ 4., 0., -3.},
{-1., 7., -8.},
{ 3., -2., 3.},
{ 1., -4., 0.},
{ 0., 0., 3.},
{ 1., 2., -3.}
};
gemmi::SMat33<double> smat{
0.000211, 0.000119, 0.0004575, -4.71e-05, 0.0001238, 3.91e-05
};
for (int i = 0; i != 10; ++i)
if (rand() % 1000 == 0)
rr[i].x = 10.;
for (auto _ : state) {
double sum = 0.;
for (int i = 0; i != 10; ++i)
sum += smat.r_u_r(rr[i]);
benchmark::DoNotOptimize(sum);
}
}
static void r_u_r2(benchmark::State& state) {
std::array<int,3> rr[10] = {
{-1, -2, -3},
{ 1, 1, -5},
{-1, 6, 3},
{ 5, -2, 9},
{ 4, 0, -3},
{-1, 7, -8},
{ 3, -2, 3},
{ 1, -4, 0},
{ 0, 0, 3},
{ 1, 2, -3}
};
for (int i = 0; i != 10; ++i)
if (rand() % 1000 == 0)
rr[i][0] = 10;
gemmi::SMat33<double> smat{
0.000211, 0.000119, 0.0004575, -4.71e-05, 0.0001238, 3.91e-05
};
for (auto _ : state) {
double sum = 0.;
for (int i = 0; i != 10; ++i)
sum += smat.r_u_r(rr[i]);
benchmark::DoNotOptimize(sum);
}
}
BENCHMARK(r_u_r1);
BENCHMARK(r_u_r2);
BENCHMARK_MAIN();
/* Output from my laptop:
--------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------
r_u_r1 30 ns 30 ns 22750626
r_u_r2 39 ns 39 ns 18032077
*/
|