forked from monahc1/ProjetFonc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
79 lines (63 loc) · 2.08 KB
/
Copy pathQueue.cpp
File metadata and controls
79 lines (63 loc) · 2.08 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
#include <iostream>
#include <memory>
#include <functional>
#include <stdexcept>
#include <chrono>
#include <vector>
#include <algorithm>
template<typename T>
class Queue {
public:
struct Node {
T value;
std::shared_ptr<Node> next;
Node(T val, std::shared_ptr<Node> nxt = nullptr) : value(val), next(nxt) {}
};
std::shared_ptr<Node> front;
std::shared_ptr<Node> back;
public:
Queue() : front(nullptr), back(nullptr) {}
Queue enqueue(T value) const {
auto newNode = std::make_shared<Node>(value);
if (back) {
back->next = newNode;
}
return Queue(front ? front : newNode, newNode);
}
Queue dequeue() const {
if (!front) throw std::out_of_range("Empty queue");
return Queue(front->next, front->next ? back : nullptr);
}
T peek() const {
if (!front) throw std::out_of_range("Empty queue");
return front->value;
}
bool isEmpty() const {
return front == nullptr;
}
private:
Queue(std::shared_ptr<Node> f, std::shared_ptr<Node> b) : front(f), back(b) {}
};
void benchmarkQueue(size_t n) {
Queue<int> queue;
std::vector<int> values(n);
std::generate(values.begin(), values.end(), []() { return std::rand() % 1000000; });
auto start = std::chrono::high_resolution_clock::now();
for (int value : values) {
queue = queue.enqueue(value);
}
size_t memoryUsage = n * (sizeof(int) + sizeof(std::shared_ptr<typename Queue<int>::Node>));
while (!queue.isEmpty()) {
queue = queue.dequeue();
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
std::cout << "Time taken to enqueue and dequeue " << n << " elements: " << duration.count() << " milliseconds\n";
std::cout << "Memory used: " << memoryUsage << " bytes\n";
}
int main() {
benchmarkQueue(100000);
benchmarkQueue(200000);
benchmarkQueue(500000);
return 0;
}