10using namespace std::chrono;
21constexpr unsigned long long operator ""_Ki(
unsigned long long n) {
return n * 1024; }
22constexpr unsigned long long operator ""_Mi(
unsigned long long n) {
return n * 1024 * 1024; }
23constexpr unsigned long long operator ""_Gi(
unsigned long long n) {
return n * 1024 * 1024 * 1024; }
25template<
typename Allocator>
27 const float fraction_deallocate,
const std::size_t size)
34 std::vector<void*> allocations(num_allocations);
35 float p_dealloc = 0.f;
36 std::size_t idx_dealloc = 0;
38 auto begin = steady_clock::now();
39 for (
auto &allocation : allocations) {
40 allocation = A.allocate(size);
41 uint8_t *ptr =
reinterpret_cast<uint8_t*
>(allocation);
44 p_dealloc += fraction_deallocate;
45 if (p_dealloc >= 1.f) {
47 A.deallocate(allocations[idx_dealloc++], size);
50 auto end = steady_clock::now();
52 std::cout <<
"fixed_allocate," << name <<
',' << size <<
',' << fraction_deallocate <<
','
53 << num_allocations << ',' << duration_cast<microseconds>(end - begin).count() / 1e3 << std::endl;
54 for (; idx_dealloc != num_allocations; ++idx_dealloc)
55 A.deallocate(allocations[idx_dealloc], size);
59template<
typename Allocator>
61 const std::size_t size)
68 std::vector<void*> allocations(num_allocations);
70 auto begin = steady_clock::now();
71 for (
auto &allocation : allocations) {
72 allocation = A.allocate(size);
73 uint8_t *ptr =
reinterpret_cast<uint8_t*
>(allocation);
76 for (
auto allocation : allocations) {
77 A.deallocate(allocation, size);
79 auto end = steady_clock::now();
81 std::cout <<
"fixed_allocate_then_deallocate," << name <<
',' << size <<
',' << 1.f <<
','
82 << num_allocations << ',' << duration_cast<microseconds>(end - begin).count() / 1e3 << std::endl;
86template<
typename Allocator>
88 const std::size_t size)
91 num_allocations <= NUM_ALLOCATIONS_STOP >> 2U;
95 std::vector<void*> allocations(num_allocations);
97 auto begin = steady_clock::now();
98 for (
auto &allocation : allocations) {
99 allocation = A.allocate(size);
100 uint8_t *ptr =
reinterpret_cast<uint8_t*
>(allocation);
103 for (
auto it = allocations.crbegin(); it != allocations.crend(); ++it) {
104 A.deallocate(*it, size);
106 auto end = steady_clock::now();
108 std::cout <<
"fixed_allocate_then_deallocate_reversed," << name <<
',' << size <<
',' << 1.f <<
','
109 << num_allocations << ',' << duration_cast<microseconds>(end - begin).count() / 1e3 << std::endl;
113template<
typename Allocator>
116 for (
float p : { 0.f, .5f, 1.f}) {
146 std::cout <<
"type,allocator,size,p_dealloc,count,time" << std::endl;
void run_benchmark_suite_for_allocator(const std::string &name, const Allocator &proto)
void run_benchmark_allocations_fixed_allocate(const std::string &name, const Allocator &proto, const float fraction_deallocate, const std::size_t size)
void run_benchmark_allocations_fixed_allocate_then_deallocate_reversed(const std::string &name, const Allocator &proto, const std::size_t size)
static constexpr std::size_t NUM_ALLOCATIONS_START
void run_benchmark_allocations_fixed_allocate_then_deallocate(const std::string &name, const Allocator &proto, const std::size_t size)
static constexpr std::size_t NUM_ALLOCATIONS_STOP
Implements a list allocator.
This allocator serves allocations using malloc/free.