Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:55840 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19174 invoked from network); 17 Oct 2011 12:57:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Oct 2011 12:57:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=felipensp@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=felipensp@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.177 as permitted sender) X-PHP-List-Original-Sender: felipensp@gmail.com X-Host-Fingerprint: 209.85.216.177 mail-qy0-f177.google.com Received: from [209.85.216.177] ([209.85.216.177:45018] helo=mail-qy0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 55/04-27630-9362C9E4 for ; Mon, 17 Oct 2011 08:57:29 -0400 Received: by qyk7 with SMTP id 7so1120769qyk.8 for ; Mon, 17 Oct 2011 05:57:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=2Jx9B6H7oPR7GdtYjO0ks5WUS2oVqu513L6EqZ7BN9o=; b=xo55SFZKwds89u6EQpR81f0EGZbifoRuo+LhHA5qVfHXY/kOBlQxXrMRRVSwXOvRLq gP9/OOSskXdk5txriiEK3sqbErtWMuNmKCsN22a+hIBP2s8/uFGTY8+yQ/KrHmoVbz6H sGe3XE1XqObnriiAaFnyoGF0sBP37YNvnWqqQ= Received: by 10.68.19.34 with SMTP id b2mr38061792pbe.60.1318856245161; Mon, 17 Oct 2011 05:57:25 -0700 (PDT) MIME-Version: 1.0 Received: by 10.142.105.14 with HTTP; Mon, 17 Oct 2011 05:57:05 -0700 (PDT) In-Reply-To: References: <201110142008.56344.arnaud.lb@gmail.com> Date: Mon, 17 Oct 2011 10:57:05 -0200 Message-ID: To: Ilia Alshanetsky Cc: Arnaud Le Blanc , PHP Internals , Andi Gutmans , Zeev Suraski , Stas Malyshev , Dmitry Stogov , Nikita Popov Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Ternary operator performance improvements From: felipensp@gmail.com (Felipe Pena) 2011/10/17 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 wr= ote: >> Hi, >> >> I've already posted this patch and it has since been reviewed and improv= ed. >> 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) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0if (true) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$b =3D $a; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0} else { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$b =3D $a; >> =C2=A0 =C2=A0 =C2=A0 =C2=A0} >> } >> >> // this takes 3.8 seconds: >> >> for ($i =3D 0; $i < 5000000; ++$i) { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0$b =3D true ? $a : $a; >> } >> >> I've tried to reduce the performance hit by avoiding the copy when possi= ble >> (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 =C2=A0 =C2=A0 =C2=A0 =C2=A00.124 >> true ? 1+0 : 0 =C2=A0 =C2=A0 =C2=A00.109 >> true ? $ary : 0 =C2=A0 =C2=A0 2.020 ! >> true ? $int : 0 =C2=A0 =C2=A0 0.103 >> true ? ${'ary'} : 0 2.290 ! >> true ?: 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 0.091 >> 1+0 ?: 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A00.086 >> $ary ?: 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 2.151 ! >> ${'var'} ?: 0 =C2=A0 =C2=A0 =C2=A0 2.317 ! >> >> With patch: >> >> true ? 1 : 0 =C2=A0 =C2=A0 =C2=A0 =C2=A00.124 >> true ? 1+0 : 0 =C2=A0 =C2=A0 =C2=A00.195 >> true ? $ary : 0 =C2=A0 =C2=A0 0.103 >> true ? $int : 0 =C2=A0 =C2=A0 0.089 >> true ? ${'ary'} : 0 0.103 >> true ?: 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 0.086 >> 1+0 ?: 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A00.159 >> $cv ?: 0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A00.090 >> ${'var'} ?: 0 =C2=A0 =C2=A0 =C2=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 = hit 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 >> > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > +1 from me too. --=20 Regards, Felipe Pena