Bug #64665 [Opn->Wfx]: str_tr is 5-10% faster than str_replace

From: Date: Thu, 18 Apr 2013 17:45:51 +0000
Subject: Bug #64665 [Opn->Wfx]: str_tr is 5-10% faster than str_replace
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-9743@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64665&edit=1

 ID:                 64665
 Updated by:         aharvey@php.net
 Reported by:        vova at 77xy dot net
 Summary:            str_tr is 5-10% faster than str_replace
-Status:             Open
+Status:             Wont fix
 Type:               Bug
 Package:            Documentation problem
 Operating System:   Ubuntu
 PHP Version:        5.4.14
 Block user comment: N
 Private report:     N

 New Comment:

They do different things — which one you use should be based on what you need to do, not
micro-optimisation.


Previous Comments:
------------------------------------------------------------------------
[2013-04-18 08:37:19] vova at 77xy dot net

Description:
------------
str_tr is 5-10% faster than str_replace. You should mention it in documentation or 
make str_replace as fast as str_tr



Test script:
---------------
<?php
 
function f1() {
  for($i=0; $i<1000000; ++$i) {
    $new_string = strtr("http://onlinecasinosdoc.com.au/",
".com", "");
  }
}
function f2() {
  for($i=0; $i<1000000; ++$i) {
    $new_string = str_replace("http://onlinecasinosdoc.com.au/",
".com", "");
  }
}
$start = microtime(true);
f1();
$stop = microtime(true);
$time1 = $stop - $start;
 
$start = microtime(true);
f2();
$stop = microtime(true);
$time2 = $stop - $start;
 
echo $time1 . "\t";
echo $time2 . "\n";

Expected result:
----------------
This behavior should be mentioned in documentation

Actual result:
--------------
You don't understand what is the difference between strtr and str_replace


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64665&edit=1


Thread (2 messages)

« previous php.doc.bugs (#9743) next »