php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63893 poor efficiency of strtr() using array with keys of very different length
Submitted: 2013-01-03 14:34 UTC Modified: 2013-01-15 20:52 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: scope at planetavent dot de Assigned: cataphract (profile)
Status: Closed Package: Strings related
PHP Version: 5.4.10 OS: Windows Server 2008 / RHEL 6.3
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: scope at planetavent dot de
New email:
PHP Version: OS:

 

 [2013-01-03 14:34 UTC] scope at planetavent dot de
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!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-07 01:21 UTC] cataphract@php.net
-Assigned To: +Assigned To: cataphract
 [2013-01-15 20:52 UTC] cataphract@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed in 5.4 and up.
 [2013-01-15 20:52 UTC] cataphract@php.net
-Status: Assigned +Status: Closed
 [2013-03-12 07:25 UTC] mariancjc at gmail dot com
With the release of 5.4.12 strtr() now triggers a Notice (Notice: Array to string conversion in 
...) if one of the array elements is an array (using the two arguments variant), even if the key 
is not used/found in the string (first argument)

so this:
<?php echo strtr('aa, bb', array(
        'aa'=>'qqq',
        'bb'=>'www',
        'cc'=>array('what', 'ever')
) );

Before 5.4.12 strtr() triggered the notice only if the key was found in the string

I don't know if this is as intended, but breaks existing application behavior (Kohana 3.3)

Regards
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC