-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrunch.cpp
60 lines (44 loc) · 1.69 KB
/
Crunch.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
#include "Crunch.h"
#include <chrono>
unsigned int Crunch::crunch( const unsigned int n_iterations ) const {
std::chrono::duration<double> diff {0};
auto start = std::chrono::high_resolution_clock::now();
// Flag to trigger the allocation
bool is_prime;
// Let's prepare the material for the allocations
unsigned int primes_size = 1;
unsigned long* primes = new unsigned long[primes_size];
primes[0] = 2;
unsigned long i = 2;
// Loop on numbers
for ( unsigned long int iiter = 0; iiter < n_iterations; iiter++ ) {
// Once at max, it returns to 0
i += 1;
// Check if it can be divided by the smaller ones
is_prime = true;
for ( unsigned long j = 2; j < i && is_prime; ++j ) {
if ( i % j == 0 ) is_prime = false;
} // end loop on numbers < than tested one
if ( is_prime ) {
// copy the array of primes (INEFFICIENT ON PURPOSE!)
unsigned int new_primes_size = 1 + primes_size;
unsigned long* new_primes = new unsigned long[new_primes_size];
for ( unsigned int prime_index = 0; prime_index < primes_size; prime_index++ ) {
new_primes[prime_index] = primes[prime_index];
}
// attach the last prime
new_primes[primes_size] = i;
// Update primes array
delete[] primes;
primes = new_primes;
primes_size = new_primes_size;
} // end is prime
} // end of while loop
// Fool Compiler optimisations:
for ( unsigned int prime_index = 0; prime_index < primes_size; prime_index++ )
if ( primes[prime_index] == 4 )
delete[] primes;
auto end = std::chrono::high_resolution_clock::now();
diff = end - start;
return (diff.count()*1000000);
}