This repository was archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhell1+3.cpp
70 lines (58 loc) · 2.87 KB
/
hell1+3.cpp
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
// Only implements the caching of hell3.cpp without the avx bulls**t.
// Some benches for my CPU (3600X) (-Ofast)
// 477 microseconds, 449 microseconds, 428 microseconds, 885 microseconds
#include <chrono>
#include <iostream>
#include <unordered_map>
std::unordered_map<int, long long int> cache;
void sum_up_to_n(int n) {
if (cache.find(n) != cache.end()) {
std::cout << "Sum of integers up to " << n << ": " << cache[n] << '\n';
return;
}
// Initialize sum variable
long long int sum = 0;
// Loop through integers from 1 to n
for (int i = 1; i <= n; ++i) {
// Add current integer to sum
sum += i;
}
cache[n] = sum;
std::cout << "Sum of integers up to " << n << ": " << sum << '\n';
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
int nums[] = {
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000};
for (int num : nums)
sum_up_to_n(num);
auto end = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
std::cout << "Program finished in " << duration << " microseconds."
<< std::endl;
return 0;
}