From:             scope at planetavent dot de
Operating system: Windows Server 2008 / RHEL 6.3
PHP version:      5.4.10
Package:          Strings related
Bug Type:         Bug
Bug description:poor efficiency of strtr() using array with keys of very 
different length

Description:
------------
As the documentation of strtr() points out, strtr "... will be the most
efficient when all the keys have the same size".

Using keys of very different lengths results in poor performance, even on
very small inputs.

If the str_repeat() for "m" in the test script is adjusted to 20000 the
resulting runtime increases to 45 seconds for strtr() while str_replace()
does not increase notably.

There are cases where the replacement array is built dynamically, so there
might be little control over the keylengths. It's easy to expand the
example such that strtr() takes several hours compared to just a few
seconds using str_replace().

Test script:
---------------
<?php

$text = str_repeat( 'm', 2000 );

$long_from_a = str_repeat( 'a', 1 );
$long_from_x = str_repeat( 'x', 1500 );

$replacements = array(
  $long_from_a => 'b',
  $long_from_x => 'y'
);

$start = microtime( true );
$result_1 = strtr( $text, $replacements );
echo "strtr: " . number_format( microtime( true ) - $start, 4 ) . "\n";

$start = microtime( true );
$result_2 = str_replace( array_keys( $replacements ), array_values(
$replacements ), $text );
echo "str_replace: " . number_format( microtime( true ) - $start, 4 ) .
"\n";

echo $result_1 === $result_2 ? "results match!\n": "no match!\n";

Expected result:
----------------
strtr: 0.0001
str_replace: 0.0001
results match!

Actual result:
--------------
strtr: 2.4203
str_replace: 0.0001
results match!

-- 
Edit bug report at https://bugs.php.net/bug.php?id=63893&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=63893&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=63893&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=63893&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=63893&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=63893&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=63893&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=63893&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=63893&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=63893&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=63893&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=63893&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=63893&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=63893&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=63893&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=63893&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=63893&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=63893&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=63893&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=63893&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=63893&r=mysqlcfg

Reply via email to