C++ Library Extensions 2022.12.09
To help learn modern C++ programming
043-allocator.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
3
4#include <cstdlib>
5
7
8auto nl = tpf::nl; // "\n"
9auto nL = tpf::nL; // "\n\n"
10auto flush = tpf::flush; // flushes the contents of the stream to std::cout
11auto endl = tpf::endl; // equivalent to std::endl
12
14
15// from this example, we conclude that
16template<typename ElementType>
17void dynamic_allocation_test(size_t test_count, size_t element_count)
18{
19 stream << "-------- Dynamic Allocation Test" << nl << endl;
20
21 stop_watch sw_external, sw_internal;
22
23 std::allocator<ElementType> alloc;
24
25 stream << "Dynamically allocate " << element_count
26 << " elements of type " << Tpf_GetTypeName(ElementType)
27 <<" using std::allocator() " << nl;
28 sw_external.reset();
29 for(size_t i = 0; i < test_count; ++i)
30 {
31 auto ptr_d = (ElementType*)alloc.allocate(element_count);
32
33 sw_internal.reset();
34 for(size_t i = 0; i < element_count; ++i)
35 ptr_d[i] = (ElementType)i;
36
37 stream << sw_internal.elapsed_time() << " - " << ptr_d[i] << nl;
38
39 alloc.deallocate(ptr_d, element_count);
40 }
41 stream << "elapsed: " << sw_external.elapsed_time() << nl << endl;
42}
43
45{
46 dynamic_allocation_test<int>(5, 1'000'000);
47}
48
49int main()
50{
51 std::shared_ptr<int[]> ptr = std::make_shared<int[]>((size_t)50);
52
54}
tpf::sstream stream
auto nL
void benchmarking_new_vs_malloc()
auto flush
auto endl
tpf::chrono_random::stop_watch stop_watch
void dynamic_allocation_test(size_t test_count, size_t element_count)
auto nl
int main()
std::string elapsed_time(bool bReset=true, TimeUnit dummy_time=TimeUnit{}) const
constexpr auto flush
Definition: tpf_output.hpp:970
constexpr auto nL
Definition: tpf_output.hpp:972
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
Stream output operators << are implemented.
#define Tpf_GetTypeName(type_arg)
A macro that returns type_arg's string name.
Definition: tpf_types.hpp:1422