forked from monahc1/ProjetFonc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
63 lines (50 loc) · 1.61 KB
/
Copy pathgraph.cpp
File metadata and controls
63 lines (50 loc) · 1.61 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
#include <iostream>
#include <chrono>
#include <vector>
#include <cstdlib>
#include <list>
static size_t totalAllocated = 0;
void* operator new(size_t size) {
totalAllocated += size;
return malloc(size);
}
void operator delete(void* memory) noexcept {
free(memory);
}
class Graph {
public:
explicit Graph(size_t vertices)
: adjacencyList(vertices) {}
void addEdge(int u, int v) {
adjacencyList[u].push_back(v);
}
static size_t getMemoryUsage() {
return totalAllocated;
}
private:
std::vector<std::list<int>> adjacencyList;
};
void benchmarkGraph(size_t vertices, size_t edges) {
totalAllocated = 0;
Graph graph(vertices);
srand((unsigned)time(nullptr));
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < edges; ++i) {
int u = std::rand() % vertices;
int v = std::rand() % vertices;
graph.addEdge(u, v);
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
size_t memoryUsage = Graph::getMemoryUsage();
std::cout << "Benchmark Results for " << vertices << " vertices and " << edges << " edges:\n";
std::cout << "Time taken: " << duration.count() << " milliseconds\n";
std::cout << "Memory used: " << memoryUsage << " bytes\n";
std::cout << "----------------------------------------\n";
}
int main() {
benchmarkGraph(1000, 5000);
benchmarkGraph(2000, 10000);
benchmarkGraph(5000, 25000);
return 0;
}