-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_engine.cpp
More file actions
62 lines (50 loc) · 1.45 KB
/
task_engine.cpp
File metadata and controls
62 lines (50 loc) · 1.45 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
#include "task_engine.hpp"
#include <chrono>
// ------------------
// Naive Engine
// ------------------
void NaiveEngine::load_tasks(int count) {
tasks.reserve(count);
for (int i = 0; i < count; ++i) {
tasks.push_back(new Task{i, i % 100});
}
}
void NaiveEngine::process_tasks() {
volatile int sink = 0;
for (auto* task : tasks) {
sink += task->payload * 2;
}
}
double NaiveEngine::benchmark(int iterations) {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
process_tasks();
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
return diff.count();
}
// ------------------
// Optimized Engine
// ------------------
void OptimizedEngine::load_tasks(int count) {
tasks.reserve(count);
for (int i = 0; i < count; ++i) {
tasks.push_back({i, i % 100});
}
}
void OptimizedEngine::process_tasks() {
volatile int sink = 0;
for (const auto& task : tasks) {
sink += task.payload * 2;
}
}
double OptimizedEngine::benchmark(int iterations) {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
process_tasks();
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
return diff.count();
}