$curTime = microtime(true) - $curTime; $sum += $curTime; } // average? $avgNaive = $sum / (double) $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 / (double) $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);
<?php // initialize search , returns resourceID for search structure $c = ahocorasick_init(array()); ahocorasick_add_patterns($c, array(array('key' => 'ab', 'value' => 'alfa'))); ahocorasick_add_patterns($c, array(array('key' => 'ac', 'value' => 'beta'))); ahocorasick_add_patterns($c, array(array('key' => 'ad', 'value' => 'gamma', 'aux' => array(1)))); ahocorasick_add_patterns($c, array(array('key' => 'ae', 'value' => 'delta'))); ahocorasick_add_patterns($c, array(array('id' => 0, 'value' => 'zeta'), array('key' => 'ag', 'value' => 'omega'), array('value' => 'lfa'))); // perform search 1 $d1 = ahocorasick_match("alFABETA gamma zetaomegaalfa!", $c); //unset($d1); // deinitialize search structure (will free memory) ahocorasick_deinit($c); var_dump($d1);
<?php echo "Going to init: \n"; $data = array(array('key' => 'ab', 'value' => 'alfa', 'ignoreCase' => true), array('key' => 'ac', 'value' => 'beta', 'ignoreCase' => true), array('key' => 'ad', 'value' => 'gamma', 'ignoreCase' => true), array('key' => 'ae', 'value' => 'delta', 'ignoreCase' => true), array('key' => 'af', 'value' => 'zeta', 'ignoreCase' => true), array('key' => 'ag', 'value' => 'omega', 'ignoreCase' => true), array('key' => 'ah', 'value' => 'lfa', 'ignoreCase' => true)); $c = ahocorasick_init($data); var_dump($c); $d = ahocorasick_match("alFABETA gammadelta delta delta!", $c); var_dump($d); $d = ahocorasick_match("alFABETAABECEDAAAA!", $c); var_dump($d); $d = ahocorasick_match("alFABETAABECEDAAAA!", $c, false); var_dump($d); $d = ahocorasick_match("alFABETAABECEDAAAA!", $c, true); var_dump($d); var_dump(ahocorasick_isValid($c)); var_dump(ahocorasick_deinit($c)); var_dump($c); if ($c) { var_dump(ahocorasick_isValid($c)); var_dump(ahocorasick_deinit($c)); }
<?php $data = array(array('key' => 'ab', 'value' => 'alfa', 'ignoreCase' => true), array('key' => 'ac', 'value' => 'beta', 'ignoreCase' => true), array('key' => 'ad', 'value' => 'gamma', 'ignoreCase' => true), array('key' => 'ae', 'value' => 'delta', 'ignoreCase' => true), array('key' => 'af', 'value' => 'zeta', 'ignoreCase' => true), array('key' => 'ag', 'value' => 'omega', 'ignoreCase' => true), array('key' => 'ah', 'value' => 'lfa', 'ignoreCase' => true)); // initialize search structure $c = ahocorasick_init($data); // perform search 1 $d1 = ahocorasick_match("alFABETA gammadelta delta delta!", $c); // perform search 2 $d2 = ahocorasick_match("alfa zeta omegaomegalfa", $c); // deinitialize search structure (will free memory) ahocorasick_deinit($c); var_dump($d1); var_dump($d2);
// Memoty test. echo "AhoCorasick struct: "; var_dump($c); $str = "alFABETA gammadelta delta delta simple pie! aux ssščř+ééžž ččř é é-é éeéee éé aux2 aux3 aux2"; $d = ahocorasick_match($str, $c); echo "AhoCorasick match for ahocorasick_match(\"{$str}\", c): "; var_dump($d); $str = "alFABETAABECEDAAAA!"; $d = ahocorasick_match($str, $c); echo "AhoCorasick match for ahocorasick_match(\"{$str}\", c): "; var_dump($d); $str = "alFABETAABECEDAAAA!"; $d = ahocorasick_match($str, $c, false); echo "AhoCorasick match for ahocorasick_match(\"{$str}\", c, false): "; var_dump($d); $str = "alFABETAABECEDAAAA!"; $d = ahocorasick_match($str, $c, true); echo "AhoCorasick match for ahocorasick_match(\"{$str}\", c, true): "; var_dump($d); echo "AhoCorasick isValid(c): "; var_dump(ahocorasick_isValid($c)); echo "AhoCorasick deinit(c): "; var_dump(ahocorasick_deinit($c)); echo "AhoCorasick struct: "; var_dump($c); if ($c) { echo "AhoCorasick isValid(c): "; var_dump(ahocorasick_isValid($c)); echo "AhoCorasick deinit(c): "; var_dump(ahocorasick_deinit($c)); }