Last active
October 30, 2018 02:15
-
-
Save monadbobo/93448b89a42737b08cbada81de75c5cd to your computer and use it in GitHub Desktop.
optmized the emplace_back of rocksdb::autovector
This file contains 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
//g++ -o2 autovcector_ben.cpp -lbenchmark -lpthread -o autovector_ben | |
#include <benchmark/benchmark.h> | |
#include "autovector.h" | |
std::vector<std::string> GetTestKeys(size_t size) { | |
std::vector<std::string> keys; | |
keys.resize(size); | |
int index = 0; | |
for (auto& key : keys) { | |
key = "item-" + std::to_string(index++); | |
} | |
return keys; | |
} | |
std::vector<std::string> string_keys = GetTestKeys(100000*2*8); | |
// Define another benchmark | |
static void BM_AutovectorPushback(benchmark::State& state) { | |
for (auto _ : state) { | |
for (int i = 0; i< 100000; ++i) { | |
autovector<std::string> as; | |
for (size_t j = 0; j < state.range(0); ++j) { | |
as.push_back(string_keys[i]); | |
} | |
} | |
} | |
} | |
BENCHMARK(BM_AutovectorPushback)->Arg(0)->Arg(1)->Arg(4)->Arg(8)->Arg(16); | |
static void BM_AutovectorEmplaceback(benchmark::State& state) { | |
for (auto _ : state) { | |
for (int i = 0; i< 100000; ++i) { | |
autovector<std::string, 8> as; | |
for (size_t j = 0; j < state.range(0); ++j) { | |
as.emplace_back(string_keys[i].c_str()); | |
} | |
} | |
} | |
} | |
BENCHMARK(BM_AutovectorEmplaceback)->Arg(0)->Arg(1)->Arg(4)->Arg(8)->Arg(16); | |
static void BM_AutovectorEmplaceback_optimize(benchmark::State& state) { | |
for (auto _ : state) { | |
for (int i = 0; i< 100000; ++i) { | |
autovector<std::string, 8> as; | |
for (size_t j = 0; j < state.range(0); ++j) { | |
as.emplace_back_optimize(string_keys[i].c_str()); | |
} | |
} | |
} | |
} | |
BENCHMARK(BM_AutovectorEmplaceback_optimize)->Arg(0)->Arg(1)->Arg(4)->Arg(8)->Arg(16); | |
BENCHMARK_MAIN(); | |
This file contains 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
#benchmark the rocksdb::autovector, the element in vector/autovecor was std::string | |
2018-10-30 09:49:40 | |
Running ./autovector_ben | |
Run on (4 X 3400 MHz CPU s) | |
CPU Caches: | |
L1 Data 32K (x2) | |
L1 Instruction 32K (x2) | |
L2 Unified 256K (x2) | |
L3 Unified 4096K (x1) | |
---------------------------------------------------------------------------- | |
Benchmark Time CPU Iterations | |
---------------------------------------------------------------------------- | |
BM_AutovectorPushback/0 7471002 ns 7471133 ns 90 | |
BM_AutovectorPushback/1 9029570 ns 9029851 ns 73 | |
BM_AutovectorPushback/4 13361825 ns 13362373 ns 51 | |
BM_AutovectorPushback/8 20416884 ns 20417729 ns 34 | |
BM_AutovectorPushback/16 151630310 ns 151636696 ns 4 | |
BM_AutovectorEmplaceback/0 7595114 ns 7595075 ns 88 | |
BM_AutovectorEmplaceback/1 11405845 ns 11406004 ns 59 | |
BM_AutovectorEmplaceback/4 23747714 ns 23748745 ns 29 | |
BM_AutovectorEmplaceback/8 38301110 ns 38302593 ns 18 | |
BM_AutovectorEmplaceback/16 184395206 ns 184402547 ns 4 | |
BM_AutovectorEmplaceback_optimize/0 7551782 ns 7552118 ns 90 | |
BM_AutovectorEmplaceback_optimize/1 11163601 ns 11163718 ns 60 | |
BM_AutovectorEmplaceback_optimize/4 23270649 ns 23270982 ns 30 | |
BM_AutovectorEmplaceback_optimize/8 36659024 ns 36660693 ns 19 | |
BM_AutovectorEmplaceback_optimize/16 166090771 ns 166092358 ns 4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment