Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:55806 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70609 invoked from network); 14 Oct 2011 19:39:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Oct 2011 19:39:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 212.199.177.89 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 212.199.177.89 il-mr1.zend.com Received: from [212.199.177.89] ([212.199.177.89:60415] helo=il-mr1.zend.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7F/90-63869-CEF889E4 for ; Fri, 14 Oct 2011 15:39:25 -0400 Received: from il-gw1.zend.com (unknown [10.1.1.22]) by il-mr1.zend.com (Postfix) with ESMTP id 53BD260746; Fri, 14 Oct 2011 21:38:02 +0200 (IST) Received: from tpl2.home (10.1.10.11) by il-ex2.zend.net (10.1.1.22) with Microsoft SMTP Server id 14.1.255.0; Fri, 14 Oct 2011 21:39:02 +0200 Message-ID: <4E988FE6.1060005@zend.com> Date: Fri, 14 Oct 2011 23:39:18 +0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0) Gecko/20110927 Thunderbird/7.0 MIME-Version: 1.0 To: Arnaud Le Blanc CC: PHP Internals , Andi Gutmans , Zeev Suraski , Stas Malyshev , Nikita Popov References: <201110142008.56344.arnaud.lb@gmail.com> In-Reply-To: <201110142008.56344.arnaud.lb@gmail.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-Originating-IP: [10.1.10.11] Subject: Re: Ternary operator performance improvements From: dmitry@zend.com (Dmitry Stogov) Hi, As I already said, I don't see any technical issues with the patch. I don't like it very mach, but it looks robust, and I don't object about it. Thanks. Dmitry. On 10/14/2011 10:08 PM, Arnaud Le Blanc wrote: > Hi, > > I've already posted this patch and it has since been reviewed and improved. > I'm re-posting it for discussion before eventually commiting it. > > The ternary operator always copies its second or third operand, which is very > slow compared to an if/else when the operand is an array for example: > > $a = range(0,9); > > // this takes 0.3 seconds here: > > for ($i = 0; $i< 5000000; ++$i) { > if (true) { > $b = $a; > } else { > $b = $a; > } > } > > // this takes 3.8 seconds: > > for ($i = 0; $i< 5000000; ++$i) { > $b = true ? $a : $a; > } > > I've tried to reduce the performance hit by avoiding the copy when possible > (patch attached). > > Benchmark: > > Without patch: (the numbers are the time taken to run the code a certain > amount of times) > > $int = 0; > $ary = array(1,2,3,4,5,6,7,8,9); > > true ? 1 : 0 0.124 > true ? 1+0 : 0 0.109 > true ? $ary : 0 2.020 ! > true ? $int : 0 0.103 > true ? ${'ary'} : 0 2.290 ! > true ?: 0 0.091 > 1+0 ?: 0 0.086 > $ary ?: 0 2.151 ! > ${'var'} ?: 0 2.317 ! > > With patch: > > true ? 1 : 0 0.124 > true ? 1+0 : 0 0.195 > true ? $ary : 0 0.103 > true ? $int : 0 0.089 > true ? ${'ary'} : 0 0.103 > true ?: 0 0.086 > 1+0 ?: 0 0.159 > $cv ?: 0 0.090 > ${'var'} ?: 0 0.089 > > > The array copying overhead is eliminated. There is however a slowdown in some > of the cases, but overall there is no completely unexpected performance hit as > it is the case currently. > > What do you think ? Is there any objection ? > > Best regards,