Created
December 13, 2017 02:19
-
-
Save BillyONeal/1f6f2ca8e19125f0677111f5559eed9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
std::vector<size_t> even_char_indexes(Parameter target) { | |
// get all the offsets to chars with even values in `target` | |
std::vector<size_t> result; | |
result.reserve(target.size()); | |
for (size_t idx = 0; idx < target.size(); ++idx) { | |
if ((target[idx] & 1) == 0) { | |
result.push_back(idx); | |
} | |
} | |
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]; | |
std::generate(testData, testData + Count, [&gen]() { | |
return static_cast<char>(static_cast<unsigned char>(gen())); | |
}); | |
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&, 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_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:18:23 | |
------------------------------------------------------------------------------------------------ | |
Benchmark Time CPU Iterations | |
------------------------------------------------------------------------------------------------ | |
benchmark_even_char_indexes<const std::string_view,8> 67 ns 67 ns 11200000 | |
benchmark_even_char_indexes<const std::string_view,16> 76 ns 78 ns 11200000 | |
benchmark_even_char_indexes<const std::string_view,64> 90 ns 90 ns 7466667 | |
benchmark_even_char_indexes<const std::string_view,256> 93 ns 94 ns 7466667 | |
benchmark_even_char_indexes<const std::string_view,1024> 89 ns 89 ns 8960000 | |
benchmark_even_char_indexes<const std::string&,8> 75 ns 75 ns 8960000 | |
benchmark_even_char_indexes<const std::string&,16> 128 ns 128 ns 5600000 | |
benchmark_even_char_indexes<const std::string&,64> 144 ns 144 ns 4977778 | |
benchmark_even_char_indexes<const std::string&,256> 144 ns 143 ns 4480000 | |
benchmark_even_char_indexes<const std::string&,1024> 144 ns 144 ns 4977778 | |
C:\Users\bion\Desktop> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment