forked from monahc1/ProjetFonc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchained.cpp
More file actions
216 lines (193 loc) · 5.98 KB
/
Copy pathchained.cpp
File metadata and controls
216 lines (193 loc) · 5.98 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
#include <memory>
#include <functional>
#include <windows.h>
#include <psapi.h>
using namespace std;
SIZE_T getMemoryUsage() {
PROCESS_MEMORY_COUNTERS memInfo;
GetProcessMemoryInfo(GetCurrentProcess(), &memInfo, sizeof(memInfo));
return memInfo.WorkingSetSize; // Memory usage in bytes
}
template<typename T>
class chained{
public:
chained() : first(nullptr) {}
void addlast(const T &v,SIZE_T &a){
addl(v,first,last,a);
}
void showc(){
showc1(first);
}
void remove(T v){
rm(v,first);
}
void insert(const T &v,int pos,SIZE_T &a){
insert1(v,first,pos,a);
}
~chained() {
Node* current = first;
while (current) {
Node* toDelete = current;
current = current->next;
delete toDelete;
}
}
private:
struct Node{
T value;
Node * next;
Node(T value) : value(value), next(nullptr) {}
};
Node * first;
Node * last;
void addl(const T &v,Node* &first,Node * &last,SIZE_T &a){
SIZE_T b=getMemoryUsage();
Node * node1=new Node(v);
if (first == nullptr) {
first = node1;
last = node1;
} else {
last->next = node1;
last = node1;
}
SIZE_T c=getMemoryUsage();
a=a+(c-b);
}
void showc1(Node * first){
if (first==nullptr){
cout<<"no list to show";
return;
}
Node * tmp=first;
while(tmp!=nullptr){
cout<<" "<<tmp->value<<" ";
tmp=tmp->next;
}
cout<<endl;
return;
}
void rm(T v, Node*& first) {
if (first == nullptr) {
cout << "List is empty, nothing to remove." << endl;
return;
}
Node* pt = first;
Node* pt1 = nullptr;
while (pt != nullptr && pt->value != v) {
pt1 = pt;
pt = pt->next;
}
if (pt == nullptr) {
cout << "Value " << v << " not found in the list." << endl;
return;
}
if (pt == first) {
first = pt->next;
} else {
pt1->next = pt->next;
}
cout << "Node with value " << v << " removed." << endl;
delete pt;
}
void insert1(const T &v,Node * &first,int pos,SIZE_T &a){
SIZE_T b=getMemoryUsage();
Node * n1=new Node(v);
SIZE_T f=0;
if(pos==0){
SIZE_T m1=getMemoryUsage();
n1->next=first;
first=n1;
SIZE_T m2=getMemoryUsage();
f=f+(m2-m1);
return;
}
if (first == nullptr && pos > 0) {
SIZE_T m1=getMemoryUsage();
first=n1;
SIZE_T m2=getMemoryUsage();
f=f+(m2-m1);
return;
}
else{
SIZE_T a1=getMemoryUsage();
int c=0;
Node * tmp=first;
while(c<pos-1 && tmp->next!=nullptr ){
SIZE_T m1=getMemoryUsage();
tmp=tmp->next;
c++;
SIZE_T m2=getMemoryUsage();
f=f+(m2-m1);
}
n1->next=tmp->next;
tmp->next=n1;
SIZE_T a2=getMemoryUsage();
f=f+(a2-a1);
}
SIZE_T c=getMemoryUsage();
a=a+(c-b)+f;
return;
}
};
void benchmarkaddlast(size_t n) {
chained<int> ch1;
std::vector<int> values(n);
std::generate(values.begin(), values.end(), []() { return std::rand() % 1000000; });
auto start = std::chrono::high_resolution_clock::now();
SIZE_T before = getMemoryUsage();
SIZE_T total2=0;
for (int value : values) {
ch1.addlast(value,total2);
}
SIZE_T after = getMemoryUsage();
total2=total2+(after-before);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout << "Time taken to addlast " << n << " elements: " << duration.count() << " milliseconds\n";
std::cout << "Memory used by addlast: " << (total2) / 1024 << " KB" << std::endl;
}
void benchmarkaddfirst(size_t n) {
chained<int> ch3;
std::vector<int> values(n);
std::generate(values.begin(), values.end(), []() { return std::rand() % 1000000; });
auto start = std::chrono::high_resolution_clock::now();
SIZE_T total1=0;
SIZE_T before=getMemoryUsage();
for (int value : values) {
ch3.insert(value,0,total1);
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
SIZE_T after=getMemoryUsage();
total1=total1+(after-before);
std::cout << "Time taken to addfirst " << n << " elements: " << duration.count() << " milliseconds\n";
std::cout << "Memory used by addfirst: " << (total1) / 1024 << " KB" << std::endl;
}
void benchmarkinsert(size_t n) {
chained<int> ch2;
std::vector<int> values(n);
std::generate(values.begin(), values.end(), []() { return std::rand() % 1000000; });
auto start = std::chrono::high_resolution_clock::now();
SIZE_T total=0;
SIZE_T before=getMemoryUsage();
for (int i=0;i<n;i++) {
ch2.insert(values[i],i/2,total);
}
;
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
SIZE_T after=getMemoryUsage();
total=total+(after-before);
std::cout << "Time taken to insert " << n << " elements: " << duration.count() << " milliseconds\n";
std::cout << "Memory used by insert: " << (total) / 1024 << " KB" << std::endl;
}
int main() {
benchmarkaddfirst(100000);
benchmarkaddlast(100000);
benchmarkinsert(100000);
return 0;
}