$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);