All pastes #16062 Raw Edit

Anonymous

public text v1 · immutable
#16062 ·published 2005-06-28 10:58 UTC
rendered paste body
<?php

	define( 'MAX', 10000 );
	define( 'EVERY', 1000 );

	$string = 'some crap here, replace this, more crap here';

	$start = getMicrotime();
	echo '<h3>str_replace</h3>';
	for( $i = 0; $i < MAX; $i++ )
	{
		str_replace( 'replace this', '', $string );
		$time = getMicrotime() - $start;
		if( $i % EVERY == 0 )
			echo $time.'<br/>';
	}

	$start = getMicrotime();
	echo '<h3>preg_replace</h3>';
	for( $i = 0; $i < MAX; $i++ )
	{
		preg_replace( '#replace this#', '', $string );
		$time = getMicrotime() - $start;
		if( $i % EVERY == 0 )
			echo $time.'<br/>';
	}


	preg_match( '#^(\d+)\.(\d+)\.(\d+)#', phpversion(), $m );
	$version = (int)$m[1]*10000 + (int)$m[2]*100 + (int)$m[3];
	if( $version >= 50000 )
	{
		$start = getMicrotime();
		echo '<h3>str_ireplace</h3>';
		for( $i = 0; $i < MAX; $i++ )
		{
			str_ireplace( 'Replace this', '', $string );
			$time = getMicrotime() - $start;
			if( $i % EVERY == 0 )
				echo $time.'<br/>';
		}

		$start = getMicrotime();
		echo '<h3>preg_replace (case insensitive)</h3>';
		for( $i = 0; $i < MAX; $i++ )
		{
			preg_replace( '#Replace this#i', '', $string );
			$time = getMicrotime() - $start;
			if( $i % EVERY == 0 )
				echo $time.'<br/>';
		}
	}

	function getMicrotime()
	{
		list( $usec, $sec ) = explode (' ', microtime() );
		return ( (float) $sec + (float) $usec );
	}

?>