-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.php
More file actions
64 lines (50 loc) · 1.69 KB
/
benchmark.php
File metadata and controls
64 lines (50 loc) · 1.69 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
53
54
55
56
57
58
59
60
61
62
63
64
<?php
include 'vendor/autoload.php';
$examples = [
'examples/hard.txt',
'examples/harder.txt',
'examples/hardest.txt',
'examples/evenharder.txt'
];
// Create our classes
$printer = new \Andywaite\Sudoku\GridPrinter();
$gridLoader = new \Andywaite\Sudoku\GridLoader();
$cellChecker = new \Andywaite\Sudoku\CellChecker();
$solvers = [
"Basic Recursive" => new \Andywaite\Sudoku\BasicRecursiveSolver($cellChecker),
"Optimised Recursive" => new \Andywaite\Sudoku\RecursiveSolverWithOptimisation($cellChecker),
"Hacky But Fast" => new \Andywaite\Sudoku\HackyButFast(),
"Hacky But Fast v2" => new \Andywaite\Sudoku\HackyButFastV2()
];
$results = [];
foreach ($examples as $example) {
foreach ($solvers as $title => $solver) {
// Load start grid from file
$grid = new \Andywaite\Sudoku\Grid();
$gridLoader->loadGrid($grid, $example);
// Solve!
$start = microtime(true);
try {
if (!$solver->solve($grid)) {
throw new Exception("Solver returned false");
}
$end = microtime(true);
$runTime = ($end - $start);
} catch (Exception $e) {
$runTime = "FAIL";
}
$results[$example][$title] =number_format($runTime, 6);
}
}
$benchmarkOutput = "# Benchmarks for solvers";
$benchmarkOutput .= "\n| |";
$blankLine = "\n|---|";
foreach ($solvers as $title => $solver) {
$benchmarkOutput .= $title."|";
$blankLine .= "---|";
}
$benchmarkOutput .= $blankLine;
foreach($results as $exampleFilename => $result) {
$benchmarkOutput .= "\n|".$exampleFilename."|".implode("|", $result)."|";
}
file_put_contents("benchmark.md", $benchmarkOutput);