Project

General

Profile

Bug #23099 » 14997-benchmark.php

Administrator Admin, 2010-09-05 16:33

 
<?php
echo("<html><head><style>* {font-family:monospace}</style></head>\r\n");
echo("<body>\r\n\r\n");


function getTimeOffset($start, $stop) {
$start = explode(" ",$start);
$stop = explode(" ",$stop);
return ($stop[0] + $stop[1]) - ($start[0] + $start[1]);
}


$original = 'foo\_bar\%';
$iterations = 100000;
echo(str_replace(' ', '&nbsp;' ,sprintf("%-8s %-10s<br>\r\n", 'Before:', $original)));


/**************
* preg_replace
**************/
$start = microtime();
for ($i=0; $i <= $iterations; $i++) {
$after = preg_replace('/[_%]/', '\\\$0', $original);
}
$stop = microtime();
echo(str_replace(' ', '&nbsp;' ,sprintf("%-8s %-10s %-15s | Parsetime: %f<br>\r\n", 'After:', $after, '(preg_replace)', getTimeOffset($start, $stop))));


/**************
* str_replace
**************/
$start = microtime();
for ($i=0; $i <= $iterations; $i++) {
$after = str_replace(array('_', '%'), array('\_', '\%'), $original);
}
$stop = microtime();
echo(str_replace(' ', '&nbsp;' ,sprintf("%-8s %-10s %-15s | Parsetime: %f<br>\r\n", 'After:', $after, '(str_replace)', getTimeOffset($start, $stop))));


/**************
* addcslashes
**************/
$start = microtime();
for ($i=0; $i <= $iterations; $i++) {
$after = addcslashes($original , '_%');
}
$stop = microtime();
echo(str_replace(' ', '&nbsp;' ,sprintf("%-8s %-10s %-15s | Parsetime: %f<br>\r\n", 'After:', $after, '(addcslashes)', getTimeOffset($start, $stop))));


echo("\r\n</body></html>");
?>
(2-2/3)