News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Fabien Potencier's Blog:
The PHP Ternary Operator Fast or not?
July 18, 2011 @ 10:35:36

In a new post Fabien Potencier looks at the ternary operator in PHP and wonders which is faster - using it or not (well, sort of...)

People like micro-optimizations. They are easy to understand, easy to apply... and useless. But some time ago, while reviewing pull requests for Twig, I read an interesting discussion about the performance of the ternary operator in PHP (thanks to @nikic for the investigation). Do you know which [example] snippet is the fastest (of course, they do exactly the same)? The right answer is: it depends.

He notes that it's all about the data being worked with. As some of his tests show (testing code included) there does end up being a difference between using it on a small and large dataset. After investigation, it was found that the ternary operator copies (copy-on-write) the value versus an "if" that just evaluates. He also mentions the new "?" version of the ternary syntax in PHP 5.3, but notes it still suffers from the same issue.

0 comments voice your opinion now!
ternary optimize speed dataset copyonwrite


blog comments powered by Disqus

Similar Posts

Sebastian Bergmann's Blog: Xdebug Performance Boost

Sameer's Blog: Refactoring: An introduction for PHP programmers

PseudoCoder.com: How To Really Use Xdebug To Speed Up Your App

PHPClasses.org: PHPNG Dramatic Speedup Features Coming in PHP 6 Release

NetTuts.com: Using Plugins to Speed Up WordPress


Community Events

Don't see your event here?
Let us know!


library framework community release version symfony2 xdebug opinion example conference extension performance series introduction podcast interview php7 voicesoftheelephpant laravel api

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework