Skip to content

Commit c299889

Browse files
committed
Add add_to_top benchmark.
1 parent 51e23dd commit c299889

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎benchmark/bench_misc.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "td/telegram/telegram_api.h"
99
#include "td/telegram/telegram_api.hpp"
1010

11+
#include "td/utils/algorithm.h"
1112
#include "td/utils/benchmark.h"
1213
#include "td/utils/common.h"
1314
#include "td/utils/logging.h"
@@ -18,6 +19,7 @@
1819
#include "td/utils/port/RwMutex.h"
1920
#include "td/utils/port/Stat.h"
2021
#include "td/utils/port/thread.h"
22+
#include "td/utils/Random.h"
2123
#include "td/utils/Slice.h"
2224
#include "td/utils/SliceBuilder.h"
2325
#include "td/utils/Status.h"
@@ -676,9 +678,40 @@ class DuplicateCheckerBenchEvenOdd final : public td::Benchmark {
676678
}
677679
};
678680

681+
BENCH(AddToTopStd, "add_to_top std") {
682+
td::vector<int> v;
683+
for (int i = 0; i < n; i++) {
684+
for (size_t j = 0; j < 10; j++) {
685+
auto value = td::Random::fast(0, 9);
686+
auto it = std::find(v.begin(), v.end(), value);
687+
if (it == v.end()) {
688+
if (v.size() == 8) {
689+
v.back() = value;
690+
} else {
691+
v.push_back(value);
692+
}
693+
it = v.end() - 1;
694+
}
695+
std::rotate(v.begin(), it, it + 1);
696+
}
697+
}
698+
}
699+
700+
BENCH(AddToTopTd, "add_to_top td") {
701+
td::vector<int> v;
702+
for (int i = 0; i < n; i++) {
703+
for (size_t j = 0; j < 10; j++) {
704+
td::add_to_top(v, 8, td::Random::fast(0, 9));
705+
}
706+
}
707+
}
708+
679709
int main() {
680710
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(DEBUG));
681711

712+
td::bench(AddToTopStdBench());
713+
td::bench(AddToTopTdBench());
714+
682715
td::bench(TlToStringUpdateFileBench());
683716
td::bench(TlToStringMessageBench());
684717

0 commit comments

Comments
 (0)