File: profiling.cpp

package info (click to toggle)
foonathan-memory 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: cpp: 12,014; xml: 139; sh: 49; makefile: 22
file content (145 lines) | stat: -rw-r--r-- 5,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2015-2020 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.

// Profiling code to check performance of allocators.

#include <iomanip>
#include <iostream>
#include <locale>

#include <foonathan/memory/allocator_storage.hpp>
#include <foonathan/memory/heap_allocator.hpp>
#include <foonathan/memory/new_allocator.hpp>
#include <foonathan/memory/memory_pool.hpp>
#include <foonathan/memory/memory_stack.hpp>

using namespace foonathan::memory;

#include "benchmark.hpp"

template <class Func, class... Allocators>
void benchmark_node(std::size_t count, std::size_t size, Allocators&... allocators)
{
    int dummy[] = {(std::cout << benchmark(Func{count}, allocators, size) << '|', 0)...};
    (void)dummy;
    std::cout << '\n';
}

template <class Func>
void benchmark_node(std::initializer_list<std::size_t> counts,
                    std::initializer_list<std::size_t> node_sizes)
{
    std::cout << "##" << Func::name() << "\n";
    std::cout << '\n';
    std::cout << "Size|Heap|New|Small|Node|Array|Stack\n";
    std::cout << "----|-----|---|-----|----|-----|-----\n";
    for (auto count : counts)
        for (auto size : node_sizes)
        {
            auto heap_alloc = [&] { return heap_allocator{}; };
            auto new_alloc  = [&] { return new_allocator{}; };

            auto small_alloc = [&] {
                return memory_pool<small_node_pool>(size, count * size + 1024);
            };
            auto node_alloc = [&] {
                return memory_pool<node_pool>(size, count * std::max(size, sizeof(char*)) + 1024);
            };
            auto array_alloc = [&] {
                return memory_pool<array_pool>(size, count * std::max(size, sizeof(char*)) + 1024);
            };

            auto stack_alloc = [&] { return memory_stack<>(count * size); };

            std::cout << count << "\\*" << size << "|";
            benchmark_node<Func>(count, size, heap_alloc, new_alloc, small_alloc, node_alloc,
                                 array_alloc, stack_alloc);
        }
    std::cout << '\n';
}

template <class Func, class Second, class... Tail>
void benchmark_node(std::initializer_list<std::size_t> counts,
                    std::initializer_list<std::size_t> node_sizes)
{
    benchmark_node<Func>(counts, node_sizes);
    benchmark_node<Second, Tail...>(counts, node_sizes);
}

template <class Func, class... Allocators>
void benchmark_array(std::size_t count, std::size_t array_size, std::size_t node_size,
                     Allocators&... allocators)
{
    int dummy[] = {
        (std::cout << benchmark(Func{count}, allocators, array_size, node_size) << '|', 0)...};
    (void)dummy;
    std::cout << '\n';
}

template <class Func>
void benchmark_array(std::initializer_list<std::size_t> counts,
                     std::initializer_list<std::size_t> node_sizes,
                     std::initializer_list<std::size_t> array_sizes)
{
    using namespace foonathan::memory;
    std::cout << "##" << Func::name() << "\n";
    std::cout << '\n';
    std::cout << "Size|Heap|New|Node|Array|Stack\n";
    std::cout << "----|-----|---|----|-----|-----\n";
    for (auto count : counts)
        for (auto node_size : node_sizes)
            for (auto array_size : array_sizes)
            {
                auto mem_needed = count * std::max(node_size, sizeof(char*)) * array_size + 1024;

                auto heap_alloc = [&] { return heap_allocator{}; };
                auto new_alloc  = [&] { return new_allocator{}; };

                auto node_alloc  = [&] { return memory_pool<node_pool>(node_size, mem_needed); };
                auto array_alloc = [&] { return memory_pool<array_pool>(node_size, mem_needed); };

                auto stack_alloc = [&] { return memory_stack<>(count * mem_needed); };

                std::cout << count << "\\*" << node_size << "\\*" << array_size << "|";
                benchmark_array<Func>(count, array_size, node_size, heap_alloc, new_alloc,
                                      node_alloc, array_alloc, stack_alloc);
            }
    std::cout << '\n';
}

template <class Func, class Second, class... Tail>
void benchmark_array(std::initializer_list<std::size_t> counts,
                     std::initializer_list<std::size_t> node_sizes,
                     std::initializer_list<std::size_t> array_sizes)
{
    benchmark_array<Func>(counts, node_sizes, array_sizes);
    benchmark_array<Second, Tail...>(counts, node_sizes, array_sizes);
}

int main(int argc, char* argv[])
{
    if (argc >= 2)
        sample_size = std::size_t(std::atoi(argv[1]));

    class comma_numpunct : public std::numpunct<char>
    {
    protected:
        virtual char do_thousands_sep() const
        {
            return ',';
        }

        virtual std::string do_grouping() const
        {
            return "\03";
        }
    };

    std::cout.imbue({std::locale(), new comma_numpunct});

    std::cout << "#Node\n\n";
    benchmark_node<single, bulk, bulk_reversed, butterfly>({256, 512, 1024}, {1, 4, 8, 256});
    std::cout << "#Array\n\n";
    benchmark_array<single, bulk, bulk_reversed, butterfly>({256, 512}, {1, 4, 8}, {1, 4, 8});
}