FAQ
Hello,

ternary operator is very nice syntactic sugar...

$foo = $cond ? $bar : $baz;

...but it may slows down scripts. When $bar is array or relative big
string, it is better to aviod sugar and use if:

if ($cond) $foo = $bar; else $foo = $baz;

and reference counting will be used.

I don't know how difficult it is to implement, but it can be very useful
speed optimization for PHP 5.3 to improve ternary operator to use
reference counting.

David Grudl

Search Discussions

  • Alexey Zakhlestin at Dec 14, 2008 at 7:59 pm

    On Sun, Dec 14, 2008 at 8:06 PM, David Grudl wrote:
    Hello,

    ternary operator is very nice syntactic sugar...

    $foo = $cond ? $bar : $baz;

    ...but it may slows down scripts. When $bar is array or relative big string,
    it is better to aviod sugar and use if:

    if ($cond) $foo = $bar; else $foo = $baz;

    and reference counting will be used.

    I don't know how difficult it is to implement, but it can be very useful
    speed optimization for PHP 5.3 to improve ternary operator to use reference
    counting.
    Do you have any numbers?



    --
    Alexey Zakhlestin
    http://www.milkfarmsoft.com/
  • Ilia Alshanetsky at Dec 14, 2008 at 8:21 pm
    While it is slower due to the use of temp vars, in my experience
    unless you have a few hundred operations involving ternary operator
    you cannot even see the difference. Even then there are typically way
    more important areas of code that need to be optimized. The only time
    you can really tell its slower is in synthetic benchmarks.

    On 14-Dec-08, at 12:06 PM, David Grudl wrote:

    Hello,

    ternary operator is very nice syntactic sugar...

    $foo = $cond ? $bar : $baz;

    ...but it may slows down scripts. When $bar is array or relative big
    string, it is better to aviod sugar and use if:

    if ($cond) $foo = $bar; else $foo = $baz;

    and reference counting will be used.

    I don't know how difficult it is to implement, but it can be very
    useful speed optimization for PHP 5.3 to improve ternary operator to
    use reference counting.

    David Grudl



    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    Ilia Alshanetsky
  • David Grudl at Dec 14, 2008 at 9:31 pm
    -------- Původní zpráva --------
    Od: Ilia Alshanetsky <ilia@prohost.org>
    While it is slower due to the use of temp vars, in my experience
    unless you have a few hundred operations involving ternary operator
    you cannot even see the difference. Even then there are typically way
    more important areas of code that need to be optimized. The only time
    you can really tell its slower is in synthetic benchmarks.
    Few hundred operations involving ternary operator is nothing unusual ;)



    -------- Původní zpráva --------
    Od: Alexey Zakhlestin <indeyets@gmail.com>
    On Sun, Dec 14, 2008 at 8:06 PM, David Grudl wrote:

    Do you have any numbers?
    $arr = range(0, 1000000); // very very huge array

    $foo = true ? $arr : NULL; // 140,00 milisec

    if (true) $foo = $arr; else $foo = NULL; // 0,02 milisec


    DG.
  • Ólafur Waage at Dec 14, 2008 at 10:41 pm
    Time:
    $arr = range(0, 1000000); // 0.6367609500885
    $foo == true ? $foo = $arr : NULL; // 3.0994415283203E-06
    if (true) $foo = $arr; else $foo = NULL; // 2.8610229492188E-06

    PHP 5.1.6

    Olafur Waage
    On Sun, Dec 14, 2008 at 9:30 PM, David Grudl wrote:

    -------- Původní zpráva --------
    Od: Ilia Alshanetsky <ilia@prohost.org>
    While it is slower due to the use of temp vars, in my experience unless
    you have a few hundred operations involving ternary operator you cannot even
    see the difference. Even then there are typically way more important areas
    of code that need to be optimized. The only time you can really tell its
    slower is in synthetic benchmarks.
    Few hundred operations involving ternary operator is nothing unusual ;)



    -------- Původní zpráva --------
    Od: Alexey Zakhlestin <indeyets@gmail.com>
    On Sun, Dec 14, 2008 at 8:06 PM, David Grudl wrote:
    Do you have any numbers?
    $arr = range(0, 1000000); // very very huge array

    $foo = true ? $arr : NULL; // 140,00 milisec

    if (true) $foo = $arr; else $foo = NULL; // 0,02 milisec


    DG.

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
  • Graham Kelly at Dec 14, 2008 at 11:17 pm
    Hi,

    I wouldn't really worry about the ternary operator too much. These kinds of
    micro optimizations really only appear to make a difference in contrived
    benchmarks and usually aren't the norm in real life applications. However,
    with that said, an optimization is an optimization. Optimizations like this
    are fairly easy to detect in code. There is an optimizer extension in
    development (pecl/optimizer) that includes an optimization which converts
    ternary operations to if-else structures. I think an extention like this is
    the better way to go for such optimizations. Plus, it includes many other
    goodies to make PHP code slightly faster :). Though, the downside is that
    pecl/optimizer still isnt completely stable.
    On Sun, Dec 14, 2008 at 4:30 PM, David Grudl wrote:


    -------- Původní zpráva --------
    Od: Ilia Alshanetsky <ilia@prohost.org>
    While it is slower due to the use of temp vars, in my experience unless
    you have a few hundred operations involving ternary operator you cannot even
    see the difference. Even then there are typically way more important areas
    of code that need to be optimized. The only time you can really tell its
    slower is in synthetic benchmarks.
    Few hundred operations involving ternary operator is nothing unusual ;)



    -------- Původní zpráva --------
    Od: Alexey Zakhlestin <indeyets@gmail.com>
    On Sun, Dec 14, 2008 at 8:06 PM, David Grudl wrote:
    Do you have any numbers?
    $arr = range(0, 1000000); // very very huge array

    $foo = true ? $arr : NULL; // 140,00 milisec

    if (true) $foo = $arr; else $foo = NULL; // 0,02 milisec


    DG.


    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedDec 14, '08 at 5:07p
activeDec 14, '08 at 11:17p
posts6
users5
websitephp.net

People

Translate

site design / logo © 2022 Grokbase