Hacker News new | past | comments | ask | show | jobs | submit login

Any decent compile-time optimizer will transform your first snippet into the second one (or better). Some languages preclude that optimization at compile time, but I presume that a JIT would also have little problem performing that optimization.

That is, one could argue that a good language is one that lets developers ignore trivial changes like this without hurting performance.




More as a reference for others, this is a fun page that highlights a few interesting optimizations GCC 4.2 is capable of making: http://ridiculousfish.com/blog/posts/will-it-optimize.html Maybe the optimizer can deduce guarantees about a function, but the writers may have just included an optimization for a specific built-in because the unoptimized form is such a common mistake...


I don't see how that would work. I'd say that the two snippets describe different intentions and using one when you want the other is a case of not saying what you mean.

A function call in a loop condition might have side effects or do something very unorthodox.


I guess this is a problem with trivial examples.

Pulling redundant work out of loops is a category of optimization that is widely used. In many cases, the optimizer can detect a lack of side effects on $list inside the loop body and perform the above optimization.


The canonical PHP implementation is a simple interpreter that cannot even dream of such optimizations. Hell, it uses unions to handle dynamic typing and hash tables to represent objects.

The implementation by facebook (HipHop) might have such an optimization though.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: