-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_test.cpp
More file actions
176 lines (150 loc) · 5.79 KB
/
memory_test.cpp
File metadata and controls
176 lines (150 loc) · 5.79 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// ----------------------------------------------------------------------------
// Header file for the memory_test class. memory_test.cpp
// Created by Ferhat Erata <ferhat.erata@yale.edu> on December 09, 2019.
// Copyright (c) 2019 Yale University. All rights reserved.
// -----------------------------------------------------------------------------
#include "memory.hpp"
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;
#define ITERATIONS 5
//-------------------------------------------------------------------
int main() {
cout << "Testing lists with " << ITERATIONS << " numbers\n" << endl;
std::cout << std::fixed;
//-------------------------------------------------------------------
// Test 1: Array list
{
auto start = std::chrono::system_clock::now();
int sz = ITERATIONS;
int* array = new int[ITERATIONS * 5];
for (int k = 0; k < sz; k++) {
array[k] = 1;
}
delete[] array;
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> seconds = end - start;
cout << "Array list: " << seconds.count() << "s" << endl;
}
//-------------------------------------------------------------------
// Test 2: Preallocated vector<int>
{
auto start = std::chrono::system_clock::now();
int sz = ITERATIONS;
{
vector<int> vec1;
vec1.reserve(sz);
for (int n = 0; n < sz; ++n)
vec1.push_back(n);
vector<int> vec2;
vec2.reserve(sz);
for (int n = 0; n < sz; ++n)
vec2.push_back(n);
vector<int> vec3;
vec3.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec3.push_back(n);
vector<int> vec4;
vec4.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec4.push_back(n);
vector<int> vec5;
vec5.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec5.push_back(n);
vector<int> vec6;
vec6.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec6.push_back(n);
vector<int> vec7;
vec7.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec7.push_back(n);
vector<int> vec8;
vec8.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec8.push_back(n);
vector<int> vec9;
vec9.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec9.push_back(n);
vector<int> vec10;
vec10.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec10.push_back(n);
vector<int> vec11;
vec11.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec11.push_back(n);
vector<int> vec12;
vec12.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec12.push_back(n);
vector<int> vec13;
vec13.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec13.push_back(n);
vector<int> vec14;
vec14.reserve(sz / 3);
for (int n = 0; n < sz; ++n)
vec14.push_back(n);
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> secs = end - start;
cout << "Preallocated vector<int>: " << secs.count() << "s" << endl;
}
{
auto start = std::chrono::system_clock::now();
int sz = ITERATIONS;
using namespace microsat;
Memory<int> memory{sz * 6};
{
std::vector<int, Allocator<int>> v1{Allocator<int>{memory}};
v1.reserve(sz);
for (int n = 1; n <= sz; ++n)
v1.push_back(n);
std::vector<int, Allocator<int>> v2{Allocator<int>{memory}};
v2.reserve(sz);
for (int n = 1; n <= sz; ++n)
v2.push_back(n);
// v(v2.clear());
// v(std::cout << v2.empty() << std::endl);
std::vector<int, Allocator<int>> v3{Allocator<int>{memory}};
v3.reserve(sz);
for (int n = 1; n <= sz; ++n)
v3.push_back(n * 3);
int* tp = memory.allocate(5);
for (int n = 0; n < 5; ++n)
tp[n] = n;
// auto v4 = Memory<int>::create_vector(tp, 5);
// for (int n = 1; n <= sz; ++n)
// v4.push_back(n - sz);
// std::cout << "\n";
int* tp1 = memory.allocate(5);
for (int n = 0; n < 5; ++n)
tp1[n] = n;
// auto v5 = Memory<int>::make_vector(tp1, 5);
// std::cout << "printing vector:\n";
// for (auto i : v5) {
// std::cout << i << " ";
// }
// std::cout << "\n";
std::vector<int, Allocator<int>> v6{Allocator<int>{memory}};
v6.reserve(sz);
for (int n = 1; n <= sz; ++n)
v6.push_back(n);
// v2.reserve(sz);
// for (int n = 1; n <= sz; ++n)
// v2.push_back(sz + n);
//
// for (int i = 0; i < memory.mem_used(); ++i) {
// std::cout << memory.get_raw_memory()[i] << "\n";
// }
}
cout << "Memory::used " << memory.mem_used() << endl;
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> secs = end - start;
cout << "Custom allocated vector<int>: " << secs.count() << "s" << endl;
}
}