Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:67301 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 87433 invoked from network); 4 May 2013 14:31:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 May 2013 14:31:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=laruence@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=laruence@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.171 as permitted sender) X-PHP-List-Original-Sender: laruence@gmail.com X-Host-Fingerprint: 209.85.217.171 mail-lb0-f171.google.com Received: from [209.85.217.171] ([209.85.217.171:37762] helo=mail-lb0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 44/7D-26044-CBB15815 for ; Sat, 04 May 2013 10:31:25 -0400 Received: by mail-lb0-f171.google.com with SMTP id u10so2318685lbi.30 for ; Sat, 04 May 2013 07:31:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:cc:content-type; bh=ViCu9Ynfbe3gYMmuo9kLcOb64QPzcWAVkwjvDGRGYlg=; b=B/jKRFqp6hEcf2dmRUaH8TCgrcRQ06nyFoHfeMUW2f3bGw1OOfhoyP4sbkxqJuVOzQ kiIN2w4ezLVk6MvN7f1magXFrhqqatPsQKmX1I4/PHoFd5nwzTQX8dvMMpyGcvB7Q5kU k122Hx3esWO3mXkB4/7jQcmPFe6ocxIn+NyUYFUg8u5eAy7msBkbz2DhkYhSrBQvnxfn lge4DJf+mWtDCs30SZEjfCC8FYrFmbRv8PPeAJWfZt9yedv1HRI1GiXahDkf95NEBNRK YuXkuHS/m/JsPHHd3AHv6vuAmzGBK9MeQ92TJv7N2xxpOWtid65tTdQvB88E1Sbt2sr1 riyg== X-Received: by 10.112.133.200 with SMTP id pe8mr5639832lbb.50.1367677882159; Sat, 04 May 2013 07:31:22 -0700 (PDT) MIME-Version: 1.0 Sender: laruence@gmail.com Received: by 10.114.11.129 with HTTP; Sat, 4 May 2013 07:31:02 -0700 (PDT) In-Reply-To: References: Date: Sat, 4 May 2013 22:31:02 +0800 X-Google-Sender-Auth: fLC89KpbMfaCLCsdr1aMqC0puBc Message-ID: Cc: PHP Internals Content-Type: multipart/alternative; boundary=047d7b343360555b3204dbe552d9 Subject: Re: [PHP-DEV] Re: [PROPOSAL]Add second to callback of preg_replace_callback From: laruence@php.net (Laruence) --047d7b343360555b3204dbe552d9 Content-Type: text/plain; charset=UTF-8 I made a RFC for this: https://wiki.php.net/rfc/second_arg_to_preg_callback thanks On Sat, May 4, 2013 at 10:28 PM, Laruence wrote: > > > > On Sat, May 4, 2013 at 9:46 PM, Anthony Ferrara wrote: > >> Laruence, >> >> > foreach ($replacements as $regex => $callback) { >> > > $str = preg_replace_callback($regex, $callback, $str); >> > > } >> > > >> > >> > So if there are 10 regex in the array, you will got 10 DO_FCALL. 10 >> times >> > arguments accept, 10 times etc... >> > >> > and AS I already said: IT is inefficient. I personally can not accept >> it. >> > >> > thanks >> > >> > >> My initial reaction was that the difference in runtime is going to be so >> insignificant that it's the very definition of a micro-optimization >> between >> the two. And unless you're doing it millions of times in a hot-loop, it's >> never even going to be realized. I was going to reply to that effect. >> >> Then I decided to write a quick test. >> >> $count = 100; >> >> $start = microtime(true); >> for ($i = 0; $i < $count; $i++) { >> $str = "abc"; >> $str = preg_replace_callback('/a/', function($a) { return >> strtoupper($a[0]); }, $str); >> $str = preg_replace_callback('/b/', function($a) { return >> strtoupper($a[0]); }, $str); >> $str = preg_replace_callback('/c/', function($a) { return >> strtoupper($a[0]); }, $str); >> $str = preg_replace_callback('/a/', function($a) { return >> strtolower($a[0]); }, $str); >> $str = preg_replace_callback('/b/', function($a) { return >> strtolower($a[0]); }, $str); >> $str = preg_replace_callback('/c/', function($a) { return >> strtolower($a[0]); }, $str); >> } >> echo "Completed in " . (microtime(true) - $start) . " Seconds\n"; >> >> $start = microtime(true); >> for ($i = 0; $i < $count; $i++) { >> $str = "abc"; >> $str = preg_replace(array( >> '/a/e', >> '/b/e', >> '/c/e', >> '/a/e', >> '/b/e', >> '/c/e', >> ), >> array( >> 'strtoupper(\'$1\')', >> 'strtoupper(\'$1\')', >> 'strtoupper(\'$1\')', >> 'strtolower(\'$1\')', >> 'strtolower(\'$1\')', >> 'strtolower(\'$1\')', >> ), >> $str >> ); >> } >> echo "Completed in " . (microtime(true) - $start) . " Seconds\n"; >> >> So basically, it's comparing the old behavior (one call to preg_replace) >> that you want to emulate, with what Nikita is suggesting. >> >> I ran it on 3v4l (http://3v4l.org/dRjtU) and the results were quite >> surprising. I was expecting the first to be slightly slower than the >> second. But the reality surprised me: >> >> Completed in 0.00076389312744141 Seconds Completed in 0.0012428760528564 >> Seconds >> >> As it turns out, calling preg_replace_callback() multiple times is almost >> 50% faster than using a single call to preg_replace(). >> >> It's likely due to the precompiled nature of closures, vs the compilation >> happening multiple times at invocation in the preg_replace /e case. >> >> But it's still worth noting that switching from the /e style to a more >> traditional preg_replace_callback implementation will get you a >> significant >> boost in performance of that. >> >> Now, keep in mind, we're talking 0.000005 seconds saved per "execution" >> (group of 6 replacements). So it's not likely to matter much or be worth >> worrying about... >> > Hey: > thanks for the benchmark, but please don't think it's useless just > because it's monior > > PHP is a web program language, let's think about a high trafiic site, > which get 100, 000, 000 pv perday. > > 100, 000, 000 * 0.0000005 = 500s perday > > and inefficent not always means performance only, in this case , you > need to define various functions for different regexs if you use loop style. > > and do you prefer array_walk or foreach when you try to iteraterly > process an array? > > > thanks > >> >> My $0.02 at least... >> >> Anthony >> > > > > -- > Laruence Xinchen Hui > http://www.laruence.com/ > -- Laruence Xinchen Hui http://www.laruence.com/ --047d7b343360555b3204dbe552d9--