Skip to content

Instantly share code, notes, and snippets.

@BillyONeal
Created December 13, 2017 02:32
Show Gist options
  • Save BillyONeal/0768c15b9504bc896efe6e7e881a927a to your computer and use it in GitHub Desktop.
Save BillyONeal/0768c15b9504bc896efe6e7e881a927a to your computer and use it in GitHub Desktop.
C:\Users\bion\Desktop>type bench.cpp
#include "benchmark/benchmark.h"
#pragma comment(lib, "benchmark")
#pragma comment(lib, "shlwapi")
#include <stddef.h>
#include <algorithm>
#include <random>
#include <string>
#include <string_view>
#include <vector>
template<class Parameter>
__declspec(noinline)
size_t even_char_indexes(Parameter target) {
// get all the offsets to chars with even values in `target`
size_t result = 0;
for (char c : target) {
if ((c & 1) == 0) {
++result;
}
}
return result;
}
template<class Parameter, size_t Count>
void benchmark_even_char_indexes(benchmark::State& state) {
// fill a string of chars to test
std::mt19937 gen(1729);
char testData[Count + 1]{};
std::generate(testData, testData + Count, [&gen]() {
// &2 prevents nulls
return static_cast<char>(static_cast<unsigned char>(gen() | 2));
});
for (auto _ : state) {
even_char_indexes<Parameter>(testData);
}
}
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string_view, 8);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string_view, 16);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string_view, 64);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string_view, 256);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string_view, 1024);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string_view, 16768);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string&, 8);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string&, 16);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string&, 64);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string&, 256);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string&, 1024);
BENCHMARK_TEMPLATE2(benchmark_even_char_indexes, const std::string&, 16768);
BENCHMARK_MAIN();
C:\Users\bion\Desktop>cl /nologo /EHsc /wd4141 /W4 /WX /O2 /std:c++17 /MD /IC:\Dev\vcpkg\installed\x86-windows\include .\bench.cpp /link /LIBPATH:C:\Dev\vcpkg\installed\x86-windows\lib
bench.cpp
C:\Users\bion\Desktop>.\bench.exe
Run on (32 X 2592 MHz CPU s)
12/12/17 18:30:48
-------------------------------------------------------------------------------------------------
Benchmark Time CPU Iterations
-------------------------------------------------------------------------------------------------
benchmark_even_char_indexes<const std::string_view,8> 8 ns 8 ns 89600000
benchmark_even_char_indexes<const std::string_view,16> 14 ns 13 ns 49777778
benchmark_even_char_indexes<const std::string_view,64> 49 ns 49 ns 11200000
benchmark_even_char_indexes<const std::string_view,256> 209 ns 210 ns 3200000
benchmark_even_char_indexes<const std::string_view,1024> 781 ns 781 ns 1120000
benchmark_even_char_indexes<const std::string_view,16768> 12537 ns 12556 ns 56000
benchmark_even_char_indexes<const std::string&,8> 7 ns 7 ns 112000000
benchmark_even_char_indexes<const std::string&,16> 55 ns 56 ns 10000000
benchmark_even_char_indexes<const std::string&,64> 70 ns 70 ns 8960000
benchmark_even_char_indexes<const std::string&,256> 141 ns 141 ns 4977778
benchmark_even_char_indexes<const std::string&,1024> 370 ns 369 ns 1947826
benchmark_even_char_indexes<const std::string&,16768> 5291 ns 5162 ns 112000
C:\Users\bion\Desktop>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment