-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.cpp
93 lines (67 loc) · 2.07 KB
/
naive.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "naive.h"
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <chrono>
int naivePrimes(int targetNumber, int numberOfThreads, bool verbose, bool time) {
int n = 0;
int temp = 0;
std::vector<std::thread> threads;
std::vector<bool> numbers;
std::chrono::time_point<std::chrono::steady_clock> t1, t2, t3, t4, t5;
t1 = std::chrono::steady_clock::now();
for (int i = 0; i < targetNumber; ++i) {
numbers.push_back(0);
}
t2 = std::chrono::steady_clock::now();
//thread passes by value, std::ref ensures invocability by wrapping
for (int i = 0; i < numberOfThreads; i++) {
threads.push_back(std::thread(findPrimes, i, numberOfThreads, std::ref(numbers)));
}
t3 = std::chrono::steady_clock::now();
for (auto &th : threads) {
th.join();
}
t4 = std::chrono::steady_clock::now();
if (verbose) {
printPrimes(numbers);
}
t5 = std::chrono::steady_clock::now();
if (time) {
std::chrono::duration<double> fillEmptyVectorTime = t2 - t1;
std::chrono::duration<double> startThreadsTime = t3 - t2;
std::chrono::duration<double> joinThreadsTime = t4 - t3;
std::chrono::duration<double> printPrimesTime = t5 - t4;
std::cout << "fillEmptyVectorTime: " << fillEmptyVectorTime.count() << std::endl;
std::cout << "startThreadsTime: " << startThreadsTime.count() << std::endl;
std::cout << "joinThreadsTime: " << joinThreadsTime.count() << std::endl;
std::cout << "printPrimesTime: " << printPrimesTime.count() << std::endl;
}
return 0;
}
void findPrimes(int startIndex, int jump, std::vector<bool>& numbers) {
int n = startIndex;
int numberSize = numbers.size();
while (n < numberSize) {
if (isPrime(n)) {
numbers[n] = true;
}
n = n + jump;
}
}
bool isPrime(int n) {
for (int temp = 2; temp < n; ++temp) {
if (n % temp == 0) {
return false;
}
}
return true;
}
void printPrimes(std::vector<bool> v) {
for (int i = 0; i < v.size(); ++i) {
if (v[i] == true) {
std::cout << i << std::endl;
}
}
}