Is Python slow?

Content:

Original link: Is Python Really That Slow? / Miguel Grinberg.

The numbers does not surprise.

I don't think they are so relevant though. Nobody writes CPU intensive code in Python. People write Python that orchestrate calls to some C or Fortran library that contains CPU intensive code.

I get very similar result on an old Xeon with Windows 10 doing (doing 10 20 30 40 48 instead of 10 20 30 40):

CPython 2.7 - 1282 s
CPython 3.11 - 925 s
PyPy 3.10 - 91 s
GraalPy 3.10 - 165 s
Rust - 11 s

But I think there is an interesting point that becomes visible by adding Groovy.

Two groovy implementations:

def fib(n) {
    return n < 2 ? n : fib(n - 1) + fib(n - 2)
}

for(arg in args) {
    n = Integer.parseInt(arg)
    printf("%d %d\n", n, fib(n));
}
and:
@groovy.transform.CompileStatic
int fib(int n) {
    return n < 2 ? n : fib(n - 1) + fib(n - 2)
}

for(arg in args) {
    n = Integer.parseInt(arg)
    printf("%d %d\n", n, fib(n));
}

Sorted and grouped results:

Dynamic types - interpreted

CPython 2.7 - 1282 s
CPython 3.11 - 925 s

Dynamic types - compiled

Groovy - 171 s
GraalPy 3.10 - 165 s
PyPy 3.10 - 91 s

Static types - compiled

Groovy with @CompileStatic - 20 s
Rust - 11 s

So:

And I am still very impressed by Groovy @CompileStatic. A scripting language where you just use static typing when needed and use the standard implementation.

Comments: