Open dagolden opened 9 years ago
The idea is not to write the most fast solution but to compare different runtimes performance.
Inlining AST functions
Inlining is possible but the code will be not supportable in a real project. In a real project you will have hundreds of math functions and they will be 10-100 lines length each. Moreover, "if" with hundreds branches will be not so fast.
Uses the "List::Util::sum0" function
Using external "sum" will be unfair for the following reasons: 1) List::Util is implemented in XS. And using XS functions does not show the Perl performance. 2) Real "SUM" will have more complex logic and will check values while iterating them and this required loop to be written.
Uses map to build the evaluated args array instead of a loop
It was intentionally written without map and ternary operator. Optimizer can understand it better. It was especially visible on previous version of JS v8(push with ternary operator in some cases prevented usage of inline caching). So, it was decided to write it the same way in other languages too just to place VM JIT optimizer(if exists) in the same conditions.
You gave an example of more idiomatic Go code, so this was offered in the same spirit. Perl can't JIT in the same way for various reasons, so people tend to code around such unavoidable limitations of the language. The JS JIT optimizations are pretty damn impressive. :-)
Agree, I'll add idiomatic Perl benchmark too
@dagolden The JS implementation uses an array for the operators table while most others use a hash. The JIT must not get all the credit ;)
Subroutine calls in Perl 5 are very expensive. Inlining the AST functions cuts the runtime by ~20-25%. But the recursive calls will add up regardless.
Here's an example if you'd like to amend what you have: https://gist.github.com/dagolden/80420895f0f517838072
Specific changes:
map
to build the evaluated args array instead of a loop -- again, that's more idiomatic.