Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64475 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80385 invoked from network); 2 Jan 2013 15:54:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jan 2013 15:54:01 -0000 Authentication-Results: pb1.pair.com header.from=scope@planetavent.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=scope@planetavent.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain planetavent.de from 89.107.189.172 cause and error) X-PHP-List-Original-Sender: scope@planetavent.de X-Host-Fingerprint: 89.107.189.172 mail.xa8.serverdomain.org Linux 2.6 Received: from [89.107.189.172] ([89.107.189.172:44840] helo=mail.xa8.serverdomain.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FE/A6-12868-81854E05 for ; Wed, 02 Jan 2013 10:54:01 -0500 Received: from mail-wi0-f172.google.com (mail-wi0-f172.google.com [209.85.212.172]) (Authenticated sender: xa8190p1) by mail.xa8.serverdomain.org (mail.xa8.serverdomain.org) with ESMTPSA id 8BBEF28481FCE for ; Wed, 2 Jan 2013 16:53:57 +0100 (CET) Received: by mail-wi0-f172.google.com with SMTP id o1so10290313wic.5 for ; Wed, 02 Jan 2013 07:53:57 -0800 (PST) MIME-Version: 1.0 Received: by 10.180.82.69 with SMTP id g5mr72444215wiy.21.1357142037373; Wed, 02 Jan 2013 07:53:57 -0800 (PST) Received: by 10.227.170.206 with HTTP; Wed, 2 Jan 2013 07:53:57 -0800 (PST) Date: Wed, 2 Jan 2013 16:53:57 +0100 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d0442838a0c223f04d250418f Subject: strtr vs. str_replace runtime From: scope@planetavent.de (Nicolai Scheer) --f46d0442838a0c223f04d250418f Content-Type: text/plain; charset=UTF-8 Hi! I stumbled upon a "problem" with the function strtr() the other day... I noticed a very long running php script, and tried to reproduce the behaviour. I traced it down to a single call of strtr doing text replacements using a search => replace array. It wasn't quit obvious why the call would take that long, so I started to investigate the issue. From the docs, it says that strtr "... will be the most efficient when all the keys have the same size". My testcase showed, that in fact I was using Keys of very different lengths (they are determined automatically, so there's no fixed list). I wrote a simple script to reproduce this behaviour: '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"; ?> On my box, this reports 2.5 seconds for strtr and 0.0 seconds for str_replace. As far as I know the only difference between str_replace and strtr is that strtr does not replace stuff in already replaced parts of the string. Might be wrong here, though. If I adjust the str_repeat for "m" from 2000 to 20000 the runtime is 45 seconds for strtr and 0.0001 for str_replace. I might have chosen the wrong tool for what I'm trying to achieve in the first place, but can anyone comment on the algorithmic complexity of strtr? This is definitely not the expected behaviour for such small inputs. Since the inputs varied and the keys where determined automatically in my original script, I was confronted with runtimes of several hours compared to just a few seconds with str_replace. If this is the expected behaviour, at least the documentation should be adjusted to state that this function is very inefficient with keylengths that are very distant from each other... Greetings Nico --f46d0442838a0c223f04d250418f--