php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64665 str_tr is 5-10% faster than str_replace
Submitted: 2013-04-18 08:37 UTC Modified: 2013-04-18 17:45 UTC
From: vova at 77xy dot net Assigned:
Status: Wont fix Package: Documentation problem
PHP Version: 5.4.14 OS: Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: vova at 77xy dot net
New email:
PHP Version: OS:

 

 [2013-04-18 08:37 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-18 17:45 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 [2013-04-18 17:45 UTC] aharvey@php.net
They do different things — which one you use should be based on what you need to do, not micro-optimisation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 11:01:30 2024 UTC