-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.php
115 lines (97 loc) · 2.74 KB
/
bench.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
use KSamuel\OptimizeMe\Fast\Search as FastSearch;
use KSamuel\OptimizeMe\Slow\Search as SlowSearch;
use KSamuel\OptimizeMe\Writer\MemoryWriter;
use KSamuel\OptimizeMe\Writer\NullWriter;
require_once 'vendor/autoload.php';
$fastSearch = new FastSearch;
$nullWriter = new NullWriter;
$memWriter = new MemoryWriter;
$slowSearch = new SlowSearch;
$filePath = 'data/users.txt';
//== check result=========================
$slowSearch->search($filePath, $memWriter);
$sResult = $memWriter->getBuffer();
$memWriter->flush();
$fastSearch->search($filePath, $memWriter);
$fResult = $memWriter->getBuffer();
if ($sResult != $fResult) {
echo 'Slow and Fast results are not equal';
exit;
}
//=========================================
//== Check Memory usage ===================
// PHP 8.2 case
if (function_exists('memory_reset_peak_usage')) {
memory_reset_peak_usage();
$slowSearch->search($filePath, $nullWriter);
$slowPeak = memory_get_peak_usage();
gc_collect_cycles();
memory_reset_peak_usage();
$fastSearch->search($filePath, $nullWriter);
$fastPeak = memory_get_peak_usage();
$slowPeak = number_format($slowPeak / 1024, 3) . ' kb';
$fastPeak = number_format($fastPeak / 1024, 3) . ' kb';
gc_collect_cycles();
} else {
$slowPeak = 'n.a. (PHP>=8.2)';
$fastPeak = 'n.a. (PHP>=8.2)';
}
//== bench =================================
$timeLimit = 1;
$slowCount = 0;
$slowTime = null;
$t = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$iStart = microtime(true);
$slowSearch->search($filePath, $nullWriter);
$iEnd = microtime(true);
if ($iEnd - $t > $timeLimit) {
break;
}
if ($i == 0) {
$slowTime = $iEnd - $iStart;
} else {
$cTime = $iEnd - $iStart;
if ($cTime < $slowTime) {
$slowTime = $cTime;
}
}
$slowCount++;
}
gc_collect_cycles();
$fastCount = 0;
$fastTime = null;
$t = microtime(true);
for ($i = 0; $i < 100000; $i++) {
$iStart = microtime(true);
$fastSearch->search($filePath, $nullWriter);
$iEnd = microtime(true);
if ($iEnd - $t > $timeLimit) {
break;
}
if ($i == 0) {
$fastTime = $iEnd - $iStart;
} else {
$cTime = $iEnd - $iStart;
if ($cTime < $fastTime) {
$fastTime = $cTime;
}
}
$fastCount++;
}
//== format results =====
$results = [
['', 'Count', 'Memory', 'Best Time'],
['Slow', $slowCount, $slowPeak, number_format($slowTime, 6) . ' s.'],
['Fast', $fastCount, $fastPeak, number_format($fastTime, 6) . ' s.'],
];
$width = [10, 10, 20, 18];
echo PHP_EOL;
foreach ($results as $index => $val) {
foreach ($val as $i => $col) {
echo str_pad($col, $width[$i], ' ', STR_PAD_LEFT);
}
echo PHP_EOL;
}
echo PHP_EOL;