Mantis - Quercus
Viewing Issue Advanced Details
2198 minor always 11-21-07 19:14 11-27-07 13:51
koreth  
nam  
normal  
closed 3.1.4  
fixed  
none    
none 3.1.4  
0002198: strtr() is very slow with an array of replacements
If you pass more than one replacement string in an array, strtr() becomes horribly slow.

<?php
header('Content-type: text/plain');
$str = str_repeat("x\n", 10000);

$start_time = microtime(true);
$x = strtr($str, "\n", '\\n');
$end_time = microtime(true);
print "simple strtr = " . ($end_time - $start_time) . "\n";

$start_time = microtime(true);
$x = strtr($str, array("\n"=>'\\n'));
$end_time = microtime(true);
print "1-element array strtr = " . ($end_time - $start_time) . "\n";

$start_time = microtime(true);
$x = strtr($str, array("\n"=>'\\n', "\r"=>'\\r'));
$end_time = microtime(true);
print "2-element array strtr = " . ($end_time - $start_time) . "\n";

$start_time = microtime(true);
$x = strtr($str, array("\n"=>'\\n', "\r"=>'\\r', "&"=>'&amp;'));
$end_time = microtime(true);
print "3-element array strtr = " . ($end_time - $start_time) . "\n";

$start_time = microtime(true);
$x = strtr($str, array("\n"=>'\\n', "\r"=>'\\r', "&"=>'&amp;', "a"=>"b"));
$end_time = microtime(true);
print "4-element array strtr = " . ($end_time - $start_time) . "\n";


Vanilla PHP output:

simple strtr = 0.00012397766113281
1-element array strtr = 0.0010809898376465
2-element array strtr = 0.0010781288146973
3-element array strtr = 0.0010428428649902
4-element array strtr = 0.0010390281677246

Quercus output:

simple strtr = 4.2999908328056335E-4
1-element array strtr = 0.0019129998981952667
2-element array strtr = 0.3932980000972748
3-element array strtr = 0.6140780001878738
4-element array strtr = 0.9110129997134209

Notes
(0002522)
nam   
11-27-07 13:51   
php/112t

performance improved by about 20 times