-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1_pr.cpp
More file actions
62 lines (51 loc) · 1.86 KB
/
lab1_pr.cpp
File metadata and controls
62 lines (51 loc) · 1.86 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 <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>
using namespace std;
using namespace std::chrono;
unsigned long long calculateFactorial(int n) {
unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
void calculateFactorialRange(int start, int end, vector<unsigned long long>& results) {
results[start] = calculateFactorial(start);
for (int i = start + 1; i <= end; ++i) {
results[i] = results[i - 1] * i;
}
}
int main() {
const int number = 65;
const int numThreads = 6;
vector<unsigned long long> results(number + 1);
vector<thread> threads;
int range = number / numThreads;
for (int numThreads = 1; numThreads <= 6; ++numThreads) {
auto start = high_resolution_clock::now();
atomic<int> threadsRunning(0);
for (int i = 0; i < numThreads; ++i) {
int threadStart = i * range + 1;
int threadEnd = (i == numThreads - 1) ? number : (i + 1) * range;
threads.emplace_back([threadStart, threadEnd, &results, &threadsRunning] {
calculateFactorialRange(threadStart, threadEnd, results);
threadsRunning.fetch_add(1, memory_order_relaxed);
});
}
for (auto& t : threads) {
t.join();
}
auto end = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << "Number of threads: " << numThreads << endl;
cout << "Factorial of " << number << " is " << results[number] << endl;
cout << "Time taken: " << duration.count() << " microseconds" << endl;
cout << "-----------------------------" << endl;
cout << endl;
threads.clear();
}
return 0;
}