Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to build a PHP/REGEX Benchmarking tool. Basically so I can compare the processing time of a regular expression to the processing time of a potentially updated regex.

The problem is, the first time I run the test- lets say it gives me a .005 time.. the second time it runs it is .0008 - obviously the regex is being cached somewhere. I would like to clear this cache out and after several days of googling I have no other recourse but to post here!

Anyone have a clue how to force the preg_match to treat each "test" as an entirely new test? I've tried clearing the session, expiring the session, I've tried running a loop of 10000 randomly generated regex tests but still, no matter what I've done, if I test the same one twice- the second time is ALWAYS faster, without any changes at all.

Thanks for reading, I look forward to input/feedback! If you are to propose a different solution, please just keep in mind that I am using PHP RegEx and the technology does have to be PHP unfortunately.

Cheers!
Dan

PHP
<?php
session_start();
try {
	if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
			clearstatcache();
			$starttime = -microtime(true);
			$subject = $_POST['subject'];
			$pattern = $_POST['regex'];
			$pattern = str_replace(array("\n", "\r"), ' ', $pattern);
			preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 0);
			echo "Error Level: ", preg_last_error(), "\r\n";
			echo "Sample Size (num characters): ", strlen($subject), "\r\n";
			echo "Regex: ", $pattern, "\r\n";
			echo "Matches: ", count($matches), "\r\n";
			$starttime += microtime(true);
			echo "Execution Time:", sprintf('%f', $starttime), "\r\n-----------------------------\r\n";
			print_r($matches);
	}
} catch (Exception $e) {
    echo "Caught exception: ",  $e->getMessage(), "\r\n";
}

session_destroy();	
?>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900