forked from monahc1/ProjetFonc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_list.cpp
More file actions
177 lines (162 loc) · 5.31 KB
/
Copy pathfixed_list.cpp
File metadata and controls
177 lines (162 loc) · 5.31 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
#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 Array{
private:
T* a;
int size;
int pos1=0;
public:
~Array(){
delete a;
}
Array(int n){
a=new T[n];
size=n;
}
void addlast(T v,SIZE_T &b){
if (pos1<size){
SIZE_T m1=getMemoryUsage();
a[pos1]=v;
pos1++;
SIZE_T m2=getMemoryUsage();
b=b+(m2-m1);
return;}
else{
cout<<"array is full"<<endl;
return;
}
}
void addfirst(T v,SIZE_T & b){
SIZE_T f=0;
if (pos1<size){
SIZE_T m1=getMemoryUsage();
int i=pos1;
for(i;i>0;i--){
SIZE_T h1=getMemoryUsage();
a[i]=a[i-1];
SIZE_T h2=getMemoryUsage();
f=f+(h2-h1);
}
a[i]=v;
pos1++;
SIZE_T m2=getMemoryUsage();
b=b+(m1-m2)+f ;
return;
}
else{
cout<<"array is full"<<endl;
return;
}
}
void insert(T v, int pos,SIZE_T &b){
if (pos<0 || pos>=size){
cout<<"index out of bounds"<<endl;
return;
}
if (pos1>=size){
cout<<"array is full"<<endl;
return;
}
else{
SIZE_T m1=getMemoryUsage();
int i=pos1;
if(pos>=pos1){
a[pos1]=v;
pos1++;
SIZE_T m2=getMemoryUsage();
b=b+(m2-m1);
return;
}
else{
SIZE_T f=0;
for(i;i>pos;i--){
SIZE_T a1=getMemoryUsage();
a[i]=a[i-1];
SIZE_T a2=getMemoryUsage();
f=f+(a2-a1);
}
a[i]=v;
pos1++;
SIZE_T m2=getMemoryUsage();
b=b+(m2-m1)+f;
return;
}
}
}
void showc(){
for(int i=0;i<pos1;i++){
cout<<" "<<a[i]<<" ";
}cout<<endl;
}
};
void benchmarkaddlast(size_t n) {
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();
Array<int> a(n);
SIZE_T total2=0;
for (int value : values) {
a.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) << " B" << std::endl;
}
void benchmarkaddfirst(size_t n) {
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();
Array<int> ch3(n);
for (int value : values) {
ch3.addfirst(value,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) << " B" << std::endl;
}
void benchmarkinsert(size_t n) {
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();
Array<int> ch2(n);
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) << " B" << std::endl;
}
int main(){
benchmarkaddfirst(10000);
benchmarkaddlast(10000);
benchmarkinsert(10000);
return 0;
}