-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CPU crunching for more realistic algorithm (#24)
* Prime finding function Function to find the nth prime number with helpers for worker calibration * Added algorithm calibration and crunching Calibration coefficient is stored on each worker and used to crunch for average runtime using prime number finder * Updated test with calibration * Made crunching runtime more predictable It uses an more naive approach that is not dependent on the density of primes * Improved crunching calibration Calibration now involves two parameters instead of one * Renamed `algorithm.jl`
- Loading branch information
Showing
5 changed files
with
61 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# meant to be consistently inefficient for crunching purposes. | ||
# just returns the largest prime less than `n_max` | ||
function find_primes(n_max::Int) | ||
primes = [2] | ||
|
||
for n in 3:n_max | ||
isPrime = true | ||
|
||
for y in 2:n÷2 | ||
if n % y == 0 | ||
isPrime = false | ||
break | ||
end | ||
end | ||
|
||
if isPrime | ||
push!(primes, n) | ||
end | ||
end | ||
|
||
return primes[end] | ||
end | ||
|
||
function benchmark_prime(n::Int) | ||
t0 = time() | ||
find_primes(n) | ||
Δt = time() - t0 | ||
|
||
return Δt | ||
end | ||
|
||
function calculate_coefficients() | ||
n_max = [1000,200_000] | ||
t_average = benchmark_prime.(n_max) | ||
|
||
return inv([n_max[i]^j for i in 1:2, j in 1:2]) * t_average | ||
end | ||
|
||
function crunch_for_seconds(t::Float64, coefficients::Vector{Float64}) | ||
(b,a) = coefficients | ||
n = ceil(Int, (-b + sqrt(abs(b^2 + 4a * t))) / 2a) | ||
find_primes(n) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters