memgraph/poc/allocator_benchmark.cpp
Dominik Gleich fcecb14545 Replace debug_assert, permanent_assert with DCHECK/CHECK
Summary:
Phase 2.

Phase 3.

Phase 4.

Phase 5.

Complete refactor.

Reviewers: florijan, mislav.bradac

Reviewed By: mislav.bradac

Subscribers: mislav.bradac, pullbot

Differential Revision: https://phabricator.memgraph.io/D895
2017-10-11 14:43:32 +02:00

60 lines
1.6 KiB
C++

#include <iostream>
#include <thread>
#include <vector>
#include "glog/logging.h"
#include "memory/allocator.hpp"
#include "memory/maker.hpp"
#include "utils/measure_time.hpp"
struct TestStructure {
TestStructure(int a, int b, int c, int d) : a(a), b(b), c(c), d(d) {}
int a, b, c, d;
};
void test_classic(int N) {
TestStructure** xs = new TestStructure*[N];
for (int i = 0; i < N; ++i) xs[i] = new TestStructure(i, i, i, i);
for (int i = 0; i < N; ++i) delete xs[i];
delete[] xs;
}
void test_fast(int N) {
TestStructure** xs = new TestStructure*[N];
for (int i = 0; i < N; ++i) xs[i] = makeme<TestStructure>(i, i, i, i);
for (int i = 0; i < N; ++i) delete xs[i];
delete[] xs;
}
int main(void) {
constexpr int n_threads = 32;
constexpr int N = 80000000 / n_threads;
auto elapsed_classic = utils::measure_time<std::chrono::milliseconds>([&]() {
std::vector<std::thread> threads;
for (int i = 0; i < n_threads; ++i)
threads.push_back(std::thread(test_classic, N));
for (auto& thread : threads) {
thread.join();
}
});
std::cout << "Classic (new): " << elapsed_classic << "ms" << std::endl;
auto elapsed_fast = utils::measure_time<std::chrono::milliseconds>([&]() {
std::vector<std::thread> threads;
for (int i = 0; i < n_threads; ++i)
threads.push_back(std::thread(test_fast, N));
for (auto& thread : threads) {
thread.join();
}
});
std::cout << "Fast (fast allocator): " << elapsed_fast << "ms" << std::endl;
CHECK(elapsed_fast < elapsed_classic)
<< "Custom fast allocator "
"has to perform faster on simple array allocation";
return 0;
}