forked from ph4r05/php_aho_corasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.php
85 lines (68 loc) · 2.08 KB
/
benchmark.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
<?php
/**
* Generates random word from given alphabet
* @param type $alphabet
* @param type $length
*/
function genRandomWord($alphabet, $length){
$alen = strlen($alphabet);
$out = "";
for($i=0; $i<$length; $i++){
$out .= $alphabet[rand(0, $alen-1)];
}
return $out;
}
// generate random data
$sampleCount = 10;
$haystackSize = 256;
$keySize = 2048;
$randomBuffers = array();
$randomKeys = array();
for($i=0; $i < $keySize; $i++){
$randomKeys[$i] = genRandomWord("abcdef", 16);
}
for($i=0; $i < $haystackSize; $i++){
$randomBuffers[$i] = genRandomWord("abcdef", 8192);
}
// do classical strpos search
$overalTime=array();
$sum = 0;
for($j = 0; $j < $sampleCount; $j++){
$curTime = microtime(true);
foreach($randomBuffers as $randomBuffer){
for($i=0; $i < $keySize; $i++){
$f = strpos($randomBuffer, $randomKeys[$i]);
}
}
$curTime = microtime(true) - $curTime;
$sum += $curTime;
}
// average?
$avgNaive = $sum/((float)$sampleCount);
printf("Classic search; sampleCount: %d; keySize: %d; timeAvg: %f\n\n", $sampleCount, $keySize, $avgNaive);
// do advanced search - aho corasick
$memStart = memory_get_usage();
$overalTime=array();
$sum = 0;
for($j = 0; $j < $sampleCount; $j++){
$curTime = microtime(true);
// init aho structure
$data = array();
for($i=0; $i < $keySize; $i++){
$data[] = array('id'=>$i, 'value'=>$randomKeys[$i], 'aux' => $randomBuffers);
}
$c = ahocorasick_init($data);
foreach($randomBuffers as $randomBuffer){
$d = ahocorasick_match($randomBuffer, $c);
}
ahocorasick_deinit($c);
$curTime = microtime(true) - $curTime;
$sum += $curTime;
unset($data);
unset($d);
}
$memStop = memory_get_usage();
$avgAho = $sum/((float)$sampleCount);
printf("AhoCorasick search; sampleCount: %d; keySize: %d; timeAvg: %f s, totalTime: %f s, memory increase: %d B\n\n",
$sampleCount, $keySize, $avgAho, $sum, $memStop-$memStart);
printf("AhoCorasick pattern matching is %f times faster than naive approach\n", $avgNaive/$avgAho);