My times:
My code:str_replace took 9.1224670410156 seconds
substr took 9.5974769592285 seconds
preg_replace took 13.881380081177 seconds
Code: Select all
<?php
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 10000000; $i++)
{
$str = '$100';
$str = substr($str, 1, strlen($str));
}
echo sprintf('str_replace took %s seconds', array_sum(explode(' ', microtime())) - $begin);
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 10000000; $i++)
{
$str = '$100';
$str = str_replace('$', null, $str);
}
echo sprintf('substr took %s seconds', array_sum(explode(' ', microtime())) - $begin);
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 10000000; $i++)
{
$str = '$100';
$str = preg_replace('/\$/', null, $str);
}
echo sprintf('preg_replace took %s seconds', array_sum(explode(' ', microtime())) - $begin);
?>And yeah...you don't actually have to supply substr() with the length of the string...I never knew that. Without strlen(), I get this:
So, it was strlen() screwing me oversubstr without strlen took 5.2599999904633 seconds