C++ Library Extensions 2022.12.09
To help learn modern C++ programming
042-dynamic_array.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2#include <tpf_matrix.hpp>
4
7
8struct alignas(16) vector_3d_pod
9{
10 size_t m_x, m_y, m_z, m_color;
11 void set(size_t x, size_t y, size_t z, size_t c)
12 {
13 this->m_x = x; this->m_y = y; this->m_z = z; this->m_color = c;
14 }
15};
16
17struct alignas(16) vector_3d_class
18{
19 size_t m_x, m_y, m_z, m_color;
20 void set(size_t x, size_t y, size_t z, size_t c)
21 {
22 this->m_x = x; this->m_y = y; this->m_z = z; this->m_color = c;
23 }
25};
26
27std::ostream& operator<<(std::ostream& os, const vector_3d_pod& obj)
28{
29 os << "< " << obj.m_x <<", " <<obj.m_y << ", " << obj.m_z << " >";
30 return os;
31}
32
33std::ostream& operator<<(std::ostream& os, const vector_3d_class& obj)
34{
35 os << "< " << obj.m_x <<", " <<obj.m_y << ", " << obj.m_z << " >";
36 return os;
37}
38
39template<typename ElementType>
41{
42 size_t test_count = 100;
43 size_t array_size = 1'000'000;
44
45 stream << "Testing std::vector<int> - " << array_size << " elements - "
46 << test_count << " times " << endl;
47
49 for(size_t i = 0; i < test_count; ++i)
50 {
51 std::vector<ElementType> v(array_size);
52 for(size_t j = 0; j < array_size; ++j)
53 v[j].set(j, j, j, j);
54
55 //auto vv = v;
56
57 // stream << vv << endl;
58 }
59 stream << " - Elapsed: " << sw.elapsed_time() << endl;
60
61 stream << "\nTesting tpf::dynamic_array<int> - " << array_size << " elements - "
62 << test_count << " times " << endl;
63 for(size_t i = 0; i < test_count; ++i)
64 {
66
67 for(size_t j = 0; j < array_size; ++j)
68 array[j].set(j, j, j, j);
69
70 //auto aa = array;
71
72 // stream << aa << endl;
73 }
74 stream << " - Elapsed: " << sw.elapsed_time() << endl;
75}
76
77int main()
78{
79 performance_test<vector_3d_pod>();
80 stream << endl << endl;
81 performance_test<vector_3d_class>();
82 stream << endl << endl;
83
84}
tpf::sstream stream
std::ostream & operator<<(std::ostream &os, const vector_3d_pod &obj)
auto endl
void performance_test()
int main()
std::string elapsed_time(bool bReset=true, TimeUnit dummy_time=TimeUnit{}) const
Implements set operations.
Definition: tpf_set.hpp:52
constexpr size_t array_size(ElementType(&array)[ElementSize]) noexcept
Definition: tpf_types.hpp:1240
constexpr auto endl
Definition: tpf_output.hpp:973
void set(size_t x, size_t y, size_t z, size_t c)
void set(size_t x, size_t y, size_t z, size_t c)
Stream output operators << are implemented.