Created
July 19, 2018 20:53
-
-
Save BillyONeal/09e71894f8a7e54455a821f1d908925a 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
| D:\build>type ..\vclib-benchmarks\benchmarks\make_pair_str.cpp | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <stdio.h> | |
| #include <string> | |
| #include <algorithm> | |
| #include <random> | |
| #include <benchmark/benchmark.h> | |
| using namespace std; | |
| __declspec(noinline) char *make_pair_c_opt(char const *s1, char const *s2) | |
| { | |
| const size_t s1Len = strlen(s1); | |
| const size_t s2Len = strlen(s2); | |
| size_t len = s1Len + s2Len + 4; | |
| char *str = (char *)malloc(len + 1); | |
| if (str) { | |
| char *next = str; | |
| *next++ = '('; | |
| memcpy(next, s1, s1Len); | |
| next += s1Len; | |
| *next++ = ','; | |
| *next++ = ' '; | |
| memcpy(next, s2, s2Len); | |
| next += s2Len; | |
| *next++ = ')'; | |
| *next++ = '\0'; | |
| } | |
| return str; | |
| } | |
| __declspec(noinline) char *make_pair_c(char const *s1, char const *s2) | |
| { | |
| int len = snprintf(nullptr, 0, "(%s, %s)", s1, s2); | |
| char *str = (char *)malloc(len + 1); | |
| if (str) { | |
| snprintf(str, len, "(%s, %s)", s1, s2); | |
| str[len] = 0; | |
| } | |
| return str; | |
| } | |
| __declspec(noinline) string make_pair_cpp(char const *s1, char const *s2) | |
| { | |
| string result("("); | |
| result += s1; | |
| result += ", "; | |
| result += s2; | |
| result += ')'; | |
| return result; | |
| } | |
| __declspec(noinline) string make_pair_cpp_reserve(char const *s1, char const *s2) | |
| { | |
| const size_t s1Len = strlen(s1); | |
| const size_t s2Len = strlen(s2); | |
| size_t len = s1Len + s2Len + 4; | |
| string result; | |
| result.reserve(len); | |
| result += '('; | |
| result.append(s1, s1Len); | |
| result += ", "; | |
| result.append(s2, s2Len); | |
| result += ')'; | |
| return result; | |
| } | |
| __declspec(noinline) string make_pair_cpp_opt(char const *s1, char const *s2) | |
| { | |
| const size_t s1Len = strlen(s1); | |
| const size_t s2Len = strlen(s2); | |
| size_t len = s1Len + s2Len + 4; | |
| string result(len, '\0'); | |
| auto next = result.begin(); | |
| *next++ = '('; | |
| next = copy_n(s1, s1Len, next); | |
| *next++ = ','; | |
| *next++ = ' '; | |
| next = copy_n(s2, s2Len, next); | |
| *next++ = ')'; | |
| return result; | |
| } | |
| string gen_random_string() { | |
| random_device rd; | |
| string result(100, '\0'); | |
| for (char& c : result) { | |
| c = static_cast<char>(rd()); | |
| } | |
| return result; | |
| } | |
| const string g_in1Str = gen_random_string(); | |
| const char * g_in1 = g_in1Str.c_str(); | |
| const string g_in2Str = gen_random_string(); | |
| const char * g_in2 = g_in2Str.c_str(); | |
| static void make_pair_c_opt_bench(benchmark::State& state) { | |
| for (auto _ : state) { | |
| free(make_pair_c_opt(g_in1, g_in2)); | |
| } | |
| } | |
| BENCHMARK(make_pair_c_opt_bench); | |
| static void make_pair_c_bench(benchmark::State& state) { | |
| for (auto _ : state) { | |
| free(make_pair_c(g_in1, g_in2)); | |
| } | |
| } | |
| BENCHMARK(make_pair_c_bench); | |
| static void make_pair_cpp_bench(benchmark::State& state) { | |
| for (auto _ : state) { | |
| (void)make_pair_cpp(g_in1, g_in2); | |
| } | |
| } | |
| BENCHMARK(make_pair_cpp_bench); | |
| static void make_pair_cpp_reserve_bench(benchmark::State& state) { | |
| for (auto _ : state) { | |
| (void)make_pair_cpp_reserve(g_in1, g_in2); | |
| } | |
| } | |
| BENCHMARK(make_pair_cpp_reserve_bench); | |
| static void make_pair_cpp_opt_bench(benchmark::State& state) { | |
| for (auto _ : state) { | |
| (void)make_pair_cpp(g_in1, g_in2); | |
| } | |
| } | |
| BENCHMARK(make_pair_cpp_opt_bench); | |
| BENCHMARK_MAIN(); | |
| D:\build>.\make_pair_str.exe | |
| 07/19/18 13:52:34 | |
| Running .\make_pair_str.exe | |
| Run on (32 X 2592 MHz CPU s) | |
| CPU Caches: | |
| L1 Data 32K (x16) | |
| L1 Instruction 32K (x16) | |
| L2 Unified 1048K (x16) | |
| L1 Data 32K (x16) | |
| L1 Instruction 32K (x16) | |
| L2 Unified 1048K (x16) | |
| L3 Unified 25952K (x1) | |
| ------------------------------------------------------------------- | |
| Benchmark Time CPU Iterations | |
| ------------------------------------------------------------------- | |
| make_pair_c_opt_bench 116 ns 117 ns 6400000 | |
| make_pair_c_bench 364 ns 361 ns 1947826 | |
| make_pair_cpp_bench 217 ns 218 ns 3446154 | |
| make_pair_cpp_reserve_bench 139 ns 138 ns 4977778 | |
| make_pair_cpp_opt_bench 218 ns 220 ns 3200000 | |
| D:\build> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment