Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Python version slower than my Perl version? [closed]

I've been a Perl guy for over 10 years but a friend convinced me to try Python and told me how much faster it is than Perl. So just for kicks I ported an app I wrote in Perl to Python and found that it runs about 3x slower. Initially my friend told me that I must have done it wrong, so I rewrote and refactored until I could rewrite and refactor no more and ... it's still a lot slower. So I did a simple test:

i = 0
j = 0

while (i < 100000000):
    i = i + 1
    j = j + 1

print j

$ time python python.py
100000000

real 0m48.100s
user 0m45.633s
sys 0m0.043s

my $i = 0;
my $j = 0;

while ($i < 100000000) {
    ++$i; # also tested $i = $i + 1 to be fair, same result
    ++$j;
}

print $j;

$ time perl perl.pl
100000000

real 0m24.757s
user 0m22.341s
sys 0m0.029s

Just under twice as slow, which doesn't seem to reflect any of the benchmarks I've seen ... is there a problem with my installation or is Python really that much slower than Perl?

like image 337
Foobarbaz Avatar asked Dec 31 '09 10:12

Foobarbaz


People also ask

Is Python slower than Perl?

Comparing the speedPerl is about 8 times faster than Python.

Is Python slow at runtime?

Internally Python code is interpreted during run time rather than being compiled to native code hence it is a bit slower.

How much Python is slower?

For reference, there is a great GitHub project evaluating performance across a dozen+ languages. The summary table is included below, and if C++ is a baseline of 1, then Python is a whopping 227x slower on the brainf test (which is a pretty interesting Turing Machine interpreter).

Is Python going to get faster?

Get excited about Python 3.11 — It's finally the time for significant performance improvements. It's no secret that Python isn't the fastest programming language out there. Well, that's about to change, or at least head in the right direction. The newest Python release — Python 3.11 — is expected to air in October 2022 ...


1 Answers

The nit-picking answer is that you should compare it to idiomatic Python:

  • The original code takes 34 seconds on my machine.
  • A for loop (FlorianH's answer) with += and xrange() takes 21.
  • Putting the whole thing in a function reduces it to 9 seconds!
    That's much faster than Perl (15 seconds on my machine)!
    Explanation: Python local vars are much faster than globals.
    (For fairness, I also tried a function in Perl - no change)
  • Getting rid of the j variable reduced it to 8 seconds:

    print sum(1 for i in xrange(100000000))

Python has the strange property that higher-level shorter code tends to be fastest :-)

But the real answer is that your "micro-benchmark" is meaningless. The real question of language speed is: what's the performance of an average real application? To know that, you should take into account:

  • Typical mix of operations in complex code. Your code doesn't contain any data structures, function calls, or OOP operations.

  • A large enough codebase to feel cache effects — many interpreter optimizations trade memory for speed, which is not measured fairly by any tiny benchmark.

  • Optimization opportunities: after you write your code, IF it's not fast enough, how much faster can you easily make it?

    E.g. how hard is it to offload the heavy lifting to effecient C libriries?

PyPy's benchmarks and Octane are good examples of what realistic language speed benchmarks look like.

If you want to talk number crunching, Python IS surprisingly popular with scientists. They love it for the simple pseudo-math syntax and short learning curve, but also for the excellent numpy library for array crunching and the ease of wrapping other existing C code.

And then there is the Psyco JIT which would probably run your toy example well under 1 second, but I can't check it now because it only works on 32-bit x86.
EDIT: Nowdays, skip Psyco and use PyPy which a cross-platform actively improving JIT.

like image 76
Beni Cherniavsky-Paskin Avatar answered Oct 11 '22 19:10

Beni Cherniavsky-Paskin