-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.js
More file actions
52 lines (44 loc) · 1.34 KB
/
Copy pathbenchmark.js
File metadata and controls
52 lines (44 loc) · 1.34 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
/**
* Benchmark runner — DO NOT MODIFY.
*
* Runs findPrimes(N) three times, takes the best time, verifies the
* count is correct, and writes result.json. Exits 0 on success, 1 on
* incorrect output or crash.
*/
const N = 50_000;
const EXPECTED_COUNT = 5133;
const RUNS = 3;
let mod;
try {
mod = require("./optimize.js");
} catch (e) {
console.error(`CRASH: failed to load optimize.js: ${e.message}`);
process.exit(1);
}
const { findPrimes } = mod;
if (typeof findPrimes !== "function") {
console.error("CRASH: optimize.js does not export a findPrimes function");
process.exit(1);
}
let bestTime = Infinity;
let count;
for (let r = 0; r < RUNS; r++) {
let primes;
const start = performance.now();
try {
primes = findPrimes(N);
} catch (e) {
console.error(`CRASH on run ${r + 1}: ${e.message}`);
process.exit(1);
}
const elapsed = performance.now() - start;
count = Array.isArray(primes) ? primes.length : -1;
if (elapsed < bestTime) bestTime = elapsed;
}
const result = { time_ms: Math.round(bestTime * 100) / 100, count, n: N, correct: count === EXPECTED_COUNT };
require("fs").writeFileSync("result.json", JSON.stringify(result, null, 2));
if (!result.correct) {
console.error(`INCORRECT: got ${count} primes up to ${N}, expected ${EXPECTED_COUNT}`);
process.exit(1);
}
console.log(`${result.time_ms}ms | ${count} primes up to ${N}`);