Skip to content

Instantly share code, notes, and snippets.

@ndevenish
Created April 21, 2020 10:11
Show Gist options
  • Save ndevenish/feb4eeacec24c4bc85c8e908e5b95f5d to your computer and use it in GitHub Desktop.
Save ndevenish/feb4eeacec24c4bc85c8e908e5b95f5d to your computer and use it in GitHub Desktop.
Simple benchmark of loop
#include <benchmark/benchmark.h>
#include <scitbx/array_family/accessors/c_grid.h>
#include <scitbx/array_family/tiny_types.h>
#include <scitbx/array_family/versa.h>
using namespace scitbx;
static void BM_loop_inside(benchmark::State& state) {
af::versa<bool, af::c_grid<3> > base(af::c_grid<3>(af::tiny<int, 3>(14, 14, 5)));
af::versa<bool, af::c_grid<3> > m(base.accessor());
for (auto _ : state) {
for (std::size_t j = 0; j < m.size(); ++j) {
benchmark::DoNotOptimize(m[j] = base[j]);
}
}
}
BENCHMARK(BM_loop_inside);
static void BM_loop_outside(benchmark::State& state) {
af::versa<bool, af::c_grid<3> > base(af::c_grid<3>(af::tiny<int, 3>(14, 14, 5)));
af::versa<bool, af::c_grid<3> > m(base.accessor());
for (auto _ : state) {
const auto size = m.size();
for (std::size_t j = 0; j < size; ++j) {
benchmark::DoNotOptimize(m[j] = base[j]);
}
}
}
BENCHMARK(BM_loop_outside);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment