I’ve heard this quite a bit about dynamically typed languages (like Python, Ruby, Groovy, etc.)
It actually makes a lot of sense. If the compiler doesn’t have to discover/lookup what something is, it should be faster, right?
Python / Cython
Check out this article on Python and Cython:
Just by statically defining the variables/params/etc there was a 30% improvement to the Python Code… Cython drastically improves beyond that by converting the Python to C and running it through a C compiler. At that point it is nearly as fast as C.
But oddly enough, Groovy is the exception. In the Groovy documentation it reads:
Will I get a performance boost if I statically type all of my fields and methods?
Actually, no. The way Groovy method selection is done, it actually takes longer if you provide lots of static type information. This could possibly change in the future, but as of Groovy 1.1 this is not the case.
That was with Groovy 1.0, but I don’t see any documentation to refute it for newer versions of Groovy. According to their documentation it will actually take longer to prove a lot of static typed info, then if you leave it without static definition.
Crazy… so how’s Groovy’s performance? That’s a hard one to track down. The language performance reports generally don’t list Groovy, but I did find a pretty descent review:
He has code samples in a variety of languages: Ruby, Python, Groovy, Java, C, C++…
I ran his code samples in Groovy, Ruby and Python 3.
- Python 3 gave a result of 93 microseconds per iteration
- Groovy performed better at 43 microseconds per iteration
- But Ruby 2.1.2 was the fastest at 32 microseconds per iteration
Note, neither of the two languages used static typing… In theory if you were to modify the Python code to be statically typed it might be 30% faster – but also in theory, Groovy doesn’t have to be statically typed to perform faster.
Also it should be noted that at the time of the test this guy did, he also used Python with Pyco – which sped it up to 33 microseconds on his machine. Pyco is depreciated these days. However Pypy and Cython would be alternatives for that added speed boost… PyPy using JIT (Just in Time compiling.)