[ create a new paste ] login | about

Link: http://codepad.org/wGAI38rK    [ raw code | fork ]

PHP, pasted on Jul 26:
function measure($function, $args = array())
{
	$start = microtime(true);
	for ($i = 0; $i < 10000; $i++)
	{
		call_user_func_array($function, $args);
	}
	$end = microtime(true);
	return $end - $start;
}

function testStrReplace($str)
{
	return str_replace(
		array_map(function($v){return".$v";}, range('A', 'Z')),
		array_map(function($v){return". $v";}, range('A', 'Z')),
		$str
	);
};

function testPregReplace($str)
{
	preg_match_all('/(\.[A-Z])/', $str, $matches);

	$patterns = array();
	$replacements = array();
	foreach ($matches[0] as $match)
	{
		$patterns[] = '/' . $match . '/';
		$replacements[] = $match[0] . ' ' . $match[1];
	}

	return preg_replace($patterns, $replacements, $str);
};

$str = "ver.2 test.Test.Hoge.Fuga";
echo measure('testStrReplace', (array) $str);
echo "\n";
echo measure('testPregReplace', (array) $str);


Create a new paste based on this one


Comments: