The Speed of Python, Ruby, and Lua's Parsers
I’ve run into some cases lately where loading a lot of generated code
in Python takes a lot of time. When I profiled my test case the
hotspot seemed to be the parser itself – that is, Python’s parser
that parses .py
source files.
I realized that I had no idea how fast the parsers for some of these languages are. As someone who is interested in parsers, this piqued my interest. I was particularly interested to see how much precompiling would help.
To satisfy my curiosity, I wrote a quick little benchmark that tries to get some rough numbers on this.
These are the results I got on my machine:
python real 0m1.521s user 0m1.184s sys 0m0.328s
ruby real 0m0.523s user 0m0.441s sys 0m0.076s
lua real 0m0.131s user 0m0.124s sys 0m0.005s
python (precompiled) real 0m0.022s user 0m0.012s sys 0m0.009s
lua (precompiled) real 0m0.005s user 0m0.002s sys 0m0.003s
Version Information:
Python 2.7.5
ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]
Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
My takeaways from this are:
- There is a surprising amount of variation here. Python’s parser probably has a lot of room for optimization. But the fact that precompiling is available probably reduces the demand for this considerably.
- Precompiling helps a lot.
- Lua continues to impress.