-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractBenchmarking.php
86 lines (74 loc) · 2.42 KB
/
AbstractBenchmarking.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
abstract class AbstractBenchmarking
{
private const MULTIPLIER_NANO_SECONDS = 1000000000;
abstract public function parseData(bool $test = false);
abstract public function part1(bool $test = false);
abstract public function part2(bool $test = false);
public function test()
{
return [
$this->part1(true),
$this->part2(true),
];
}
public function benchmarkParseData(): array
{
$bestTimeParse = PHP_INT_MAX;
for ($i = 0; $i < 100; $i++) {
$start = round(microtime(true) * self::MULTIPLIER_NANO_SECONDS);
$this->parseData();
$end = round(microtime(true) * self::MULTIPLIER_NANO_SECONDS);
$time = $end - $start;
if ($time < $bestTimeParse) {
$bestTimeParse = $time;
}
}
return $this->getTimeAndNotationForOutput($bestTimeParse);
}
public function benchmark($numberOfRuns): array
{
$bestTimePart1 = PHP_INT_MAX;
$bestTimePart2 = PHP_INT_MAX;
for ($i = 0; $i < $numberOfRuns; $i++) {
$start = round(microtime(true) * self::MULTIPLIER_NANO_SECONDS);
$this->part1();
$end = round(microtime(true) * self::MULTIPLIER_NANO_SECONDS);
$time = $end - $start;
if ($time < $bestTimePart1) {
$bestTimePart1 = $time;
}
}
for ($i = 0; $i < $numberOfRuns; $i++) {
$start = round(microtime(true) * self::MULTIPLIER_NANO_SECONDS);
$this->part2();
$end = round(microtime(true) * self::MULTIPLIER_NANO_SECONDS);
$time = $end - $start;
if ($time < $bestTimePart2) {
$bestTimePart2 = $time;
}
}
return [
$this->getTimeAndNotationForOutput($bestTimePart1),
$this->getTimeAndNotationForOutput($bestTimePart2)
];
}
private function getTimeAndNotationForOutput($ns): array
{
$time = $ns / self::MULTIPLIER_NANO_SECONDS;
$notation = 's';
if ($time < 1) {
$time *= 1000;
$notation = 'ms';
}
if ($time < 1) {
$time *= 1000;
$notation = 'μs';
}
if ($time < 1) {
$time *= 1000;
$notation = 'ns';
}
return [number_format(round($time, 3),3), $notation, $ns];
}
}