Last active
March 1, 2021 01:24
-
-
Save jasdev/7b1ce20fe15c68c419b52955f1b85da5 to your computer and use it in GitHub Desktop.
Initial prime monomial search benchmark.
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
import Benchmark | |
let primeMonomialSuite = BenchmarkSuite( | |
name: "Prime monomial search.", | |
settings: TimeUnit(.s), Iterations(25) | |
) { suite in | |
suite.benchmark("Initial approach to a monomial search.") { | |
let possiblePrimeLastDigits = ["1", "3", "7", "9"] | |
let ms = (3...9).reversed() | |
var largestPrimeMonomial: UInt? | |
outerLoop: for m in ms { | |
let mLengthMonomials = (1...m) | |
.reversed() | |
.map(String.init) | |
.permutations() | |
for monomial in mLengthMonomials { | |
guard | |
possiblePrimeLastDigits.contains(monomial.last!), | |
let intMonomial = UInt(monomial.joined()), | |
intMonomial > (largestPrimeMonomial ?? 0), | |
intMonomial.isPrime() | |
else { continue } | |
largestPrimeMonomial = intMonomial | |
break outerLoop | |
} | |
} | |
assert(largestPrimeMonomial == 7_652_413) // Spoiler alert hah. | |
} | |
} | |
Benchmark.main([primeMonomialSuite]) | |
// Outputs (in release mode on a 13-inch, ’20 MBP, 2.3 GHz Quad-Core Intel Core i7, 32 GB RAM) | |
// ``` | |
// running Prime monomial search.: Initial approach to a monomial search.... done! (25179.99 ms) | |
// | |
// name time std iterations | |
// -------------------------------------------------------------------------------------------- | |
// Prime monomial search..Initial approach to a monomial search. 1.005 s ± 4.93 % 25 | |
// ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment