Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:55839 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16806 invoked from network); 17 Oct 2011 12:52:26 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Oct 2011 12:52:26 -0000 Authentication-Results: pb1.pair.com smtp.mail=ilia@prohost.org; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ilia@prohost.org; sender-id=pass Received-SPF: pass (pb1.pair.com: domain prohost.org designates 209.85.215.170 as permitted sender) X-PHP-List-Original-Sender: ilia@prohost.org X-Host-Fingerprint: 209.85.215.170 mail-ey0-f170.google.com Received: from [209.85.215.170] ([209.85.215.170:63570] helo=mail-ey0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F9/83-27630-9052C9E4 for ; Mon, 17 Oct 2011 08:52:25 -0400 Received: by eyg7 with SMTP id 7so2833801eyg.29 for ; Mon, 17 Oct 2011 05:52:22 -0700 (PDT) MIME-Version: 1.0 Received: by 10.14.41.20 with SMTP id g20mr1184040eeb.165.1318855941663; Mon, 17 Oct 2011 05:52:21 -0700 (PDT) Received: by 10.14.127.136 with HTTP; Mon, 17 Oct 2011 05:52:21 -0700 (PDT) In-Reply-To: <201110142008.56344.arnaud.lb@gmail.com> References: <201110142008.56344.arnaud.lb@gmail.com> Date: Mon, 17 Oct 2011 08:52:21 -0400 Message-ID: To: Arnaud Le Blanc Cc: PHP Internals , Andi Gutmans , Zeev Suraski , Stas Malyshev , Dmitry Stogov , Nikita Popov Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Ternary operator performance improvements From: ilia@prohost.org (Ilia Alshanetsky) Seems like a good patch, +1 from me on inclusion into 5.4/HEAD. On Fri, Oct 14, 2011 at 2:08 PM, Arnaud Le Blanc wrot= e: > Hi, > > I've already posted this patch and it has since been reviewed and improve= d. > 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 =3D range(0,9); > > // this takes 0.3 seconds here: > > for ($i =3D 0; $i < 5000000; ++$i) { > =A0 =A0 =A0 =A0if (true) { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$b =3D $a; > =A0 =A0 =A0 =A0} else { > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$b =3D $a; > =A0 =A0 =A0 =A0} > } > > // this takes 3.8 seconds: > > for ($i =3D 0; $i < 5000000; ++$i) { > =A0 =A0 =A0 =A0$b =3D true ? $a : $a; > } > > I've tried to reduce the performance hit by avoiding the copy when possib= le > (patch attached). > > Benchmark: > > Without patch: (the numbers are the time taken to run the code a certain > amount of times) > > $int =3D 0; > $ary =3D array(1,2,3,4,5,6,7,8,9); > > true ? 1 : 0 =A0 =A0 =A0 =A00.124 > true ? 1+0 : 0 =A0 =A0 =A00.109 > true ? $ary : 0 =A0 =A0 2.020 ! > true ? $int : 0 =A0 =A0 0.103 > true ? ${'ary'} : 0 2.290 ! > true ?: 0 =A0 =A0 =A0 =A0 =A0 0.091 > 1+0 ?: 0 =A0 =A0 =A0 =A0 =A0 =A00.086 > $ary ?: 0 =A0 =A0 =A0 =A0 =A0 2.151 ! > ${'var'} ?: 0 =A0 =A0 =A0 2.317 ! > > With patch: > > true ? 1 : 0 =A0 =A0 =A0 =A00.124 > true ? 1+0 : 0 =A0 =A0 =A00.195 > true ? $ary : 0 =A0 =A0 0.103 > true ? $int : 0 =A0 =A0 0.089 > true ? ${'ary'} : 0 0.103 > true ?: 0 =A0 =A0 =A0 =A0 =A0 0.086 > 1+0 ?: 0 =A0 =A0 =A0 =A0 =A0 =A00.159 > $cv ?: 0 =A0 =A0 =A0 =A0 =A0 =A00.090 > ${'var'} ?: 0 =A0 =A0 =A0 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 h= it as > it is the case currently. > > What do you think ? Is there any objection ? > > Best regards, > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >