-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
benchmark(c-rbtree rbtree-bench.cpp) | ||
gtest(c-rbtree rbtree-test.cpp) | ||
benchmark(c-avltree avltree-bench.cpp) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <benchmark/benchmark.h> | ||
#include <cstring> | ||
#include <random> | ||
|
||
#define NO_STD 0 | ||
#define AVL_IMPLEMENTATION | ||
extern "C" { | ||
#include <data-structure/avltree.h> | ||
} | ||
|
||
std::vector<i32> keys; | ||
|
||
std::vector<i32> generate_nums(size_t length, ssize_t seed) { | ||
std::default_random_engine rng(seed); | ||
std::vector<i32> result; | ||
result.reserve(length); | ||
avltree_t tree = null; | ||
for (size_t i = 0; i < length;) { | ||
i32 key = rng(); | ||
if (avltree_get_node(tree, key) == null) { | ||
result.push_back(key); | ||
avltree_insert(tree, key, null); | ||
i++; | ||
} | ||
} | ||
avltree_free(tree); | ||
return result; | ||
} | ||
|
||
static void BM_INSERT(benchmark::State &state) { | ||
::keys = generate_nums(10000, 114514); | ||
for (auto _ : state) { | ||
avltree_t tree = null; | ||
for (const auto key : keys) { | ||
avltree_insert(tree, key, null); | ||
} | ||
benchmark::DoNotOptimize(tree); | ||
avltree_free(tree); | ||
} | ||
} | ||
|
||
static void BM_GET(benchmark::State &state) { | ||
avltree_t tree = null; | ||
for (const auto key : keys) { | ||
avltree_insert(tree, key, null); | ||
} | ||
for (auto _ : state) { | ||
for (const auto key : keys) { | ||
auto val = avltree_get(tree, key); | ||
benchmark::DoNotOptimize(val); | ||
} | ||
} | ||
avltree_free(tree); | ||
} | ||
|
||
static void BM_DELETE(benchmark::State &state) { | ||
for (auto _ : state) { | ||
avltree_t tree = null; | ||
for (const auto key : keys) { | ||
avltree_insert(tree, key, null); | ||
} | ||
for (const auto key : keys) { | ||
avltree_delete(tree, key); | ||
} | ||
benchmark::DoNotOptimize(tree); | ||
} | ||
} | ||
|
||
BENCHMARK(BM_INSERT); | ||
BENCHMARK(BM_GET); | ||
BENCHMARK(BM_DELETE); | ||
|
||
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