Live data from Hacker News

Why Python Is Slow: Looking Under the Hood

jakevdp.github.io

41–50 of 156 posts

Re: Why Python Is Slow: Looking Under the Hood

#41

Pharo 3's results (JIT enabled VM) [(1 to: 100000000) sum] timeToRun 0:00:00:07.335 http://pharo.org Version 4 with new VM due in 2015. Will perform much much better as with the range used, we need to use LargeIntegers as the 32 bit VM must promote to LargeInteger objects. With the 64bit VM, all fits in.

> pypy -mtimeit 'sum(xrange(1, 100000001))' 10 loops, best of 3: 153 msec per loop however the context of TFA is scientific computing, hence pypy being ignored/dismissed

We can do it this was if needed.

http://clementbera.wordpress.com/2013/06/19/optimizing-pharo...

Re: Why Python Is Slow: Looking Under the Hood

#42
post #26

Earlier quoted context omitted.

FWIW, wikipedia says "The main Python implementation, named CPython, [...] compiles Python programs into intermediate bytecode, which is executed by the virtual machine." I'm not sure why people call Python's VM an interpreter, but it's definitely interpreting byte code, not the source directly. This is very different from Perl, where a line of code isn't parseable until you know the values of the variables, e.g. wha…

> This is very different from Perl, where a line of code isn't parseable until you know the values of the variables Perl is crazy, but I don't think it's that crazy. From perlcompile( http://perldoc.perl.org/5.8.9/perlcompile.html ) Perl has always had a compiler: your source is compiled into an internal form (a parse tree) which is then optimized before being run. If what you say is true, it would not be possible to…

Apparently, Perl is that crazy. See https://news.ycombinator.com/item?id=5770531

Basically, you can't always parse a Perl program without running it. There is a subset that you can, not not everything.

Re: Why Python Is Slow: Looking Under the Hood

#43

Is it just me who sees a large chunk of these as flaws in the Python implementation as opposed to the Python language ? Dynamic typing can often be optimized at the compilation stage - and yes, Python has a compilation stage - this particular example is basic type inference, for example. Even more complex examples can be optimized by emitting specialized versions for the types that the compiler can see it will be cal…

Is it just me who sees a large chunk of these as flaws in the Python implementation as opposed to the Python language?

There are a number of design decision in python language that makes it inherently hard to write a fast python implementation.

Re: Why Python Is Slow: Looking Under the Hood

#44

A related question is: why is Perl so fast? Quite a few years ago I wrote a little Runge-Kutta solver in Perl for some simulation work. It seemed like a good idea at the time. The equations of motion had to be integrated over a very long time, and it could take hours for a single run (still much faster than the Monte Carlo it was being used to do a sanity-check on). I re-wrote everything in C++, and picked up less th…

I'm not sure your results are typical. In microbenchmarks, Perl is 2-125x slower than C++: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... And Java is quite a bit faster than Perl too: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... Perl isn't really that fast. It's faster than Python in most cases, but gets beat by Lua pretty consistently: http://benchmarksgame.alioth.debian.org/u32/b…

I'm not sure microbenchmarks are typical.

Re: Why Python Is Slow: Looking Under the Hood

#45

A related question is: why is Perl so fast? Quite a few years ago I wrote a little Runge-Kutta solver in Perl for some simulation work. It seemed like a good idea at the time. The equations of motion had to be integrated over a very long time, and it could take hours for a single run (still much faster than the Monte Carlo it was being used to do a sanity-check on). I re-wrote everything in C++, and picked up less th…

If you're doing numerical computation Numpy will be extremely fast. Most of it is a thin layer over C code so you get the best of both worlds(unless you're the Numpy maintainers).

Re: Why Python Is Slow: Looking Under the Hood

#46
post #26

Earlier quoted context omitted.

There are three primary methods of running code: interpretation, compilation to object code, and running on a VM/JIT. Obviously python is interpreted by any meaningful definition of interpretation and thus it is not compiled in the typically sense of the word (at least not in the implementation everyone uses). The reason human language is so expressive is because we can leave out a lot of context and formalism that i…

FWIW, wikipedia says "The main Python implementation, named CPython, [...] compiles Python programs into intermediate bytecode, which is executed by the virtual machine." I'm not sure why people call Python's VM an interpreter, but it's definitely interpreting byte code, not the source directly. This is very different from Perl, where a line of code isn't parseable until you know the values of the variables, e.g. wha…

Did you try running that?

  [13:06:36] ~$perl -e 'whatever / 25 ; die "this dies!";'
  this dies! at -e line 1.
  [13:06:36] ~$

  [13:06:38] ~$perl -e "use strict; use warnings; whatever / 25 ; die "this dies!";"
  Useless use of division (/) in void context at -e line 1.
  Bareword "whatever" not allowed while "strict subs" in use at -e line 1.
  Execution of -e aborted due to compilation errors.
  [13:06:38] ~$
After removing the comment the former parses just fine and died with "this dies!". The latter parsed and found a syntax error.

Re: Why Python Is Slow: Looking Under the Hood

#47
post #26

Earlier quoted context omitted.

FWIW, wikipedia says "The main Python implementation, named CPython, [...] compiles Python programs into intermediate bytecode, which is executed by the virtual machine." I'm not sure why people call Python's VM an interpreter, but it's definitely interpreting byte code, not the source directly. This is very different from Perl, where a line of code isn't parseable until you know the values of the variables, e.g. wha…

> This is very different from Perl, where a line of code isn't parseable until you know the values of the variables Perl is crazy, but I don't think it's that crazy. From perlcompile( http://perldoc.perl.org/5.8.9/perlcompile.html ) Perl has always had a compiler: your source is compiled into an internal form (a parse tree) which is then optimized before being run. If what you say is true, it would not be possible to…

[deleted]

Re: Why Python Is Slow: Looking Under the Hood

#48

Earlier quoted context omitted.

I'm not sure your results are typical. In microbenchmarks, Perl is 2-125x slower than C++: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... And Java is quite a bit faster than Perl too: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... Perl isn't really that fast. It's faster than Python in most cases, but gets beat by Lua pretty consistently: http://benchmarksgame.alioth.debian.org/u32/b…

I'm not sure microbenchmarks are typical.

I think they are a pretty good representation of the performance of doing something directly in the language (as opposed to just calling into lower-level libraries written in a different language).

Re: Why Python Is Slow: Looking Under the Hood

#49

A related question is: why is Perl so fast? Quite a few years ago I wrote a little Runge-Kutta solver in Perl for some simulation work. It seemed like a good idea at the time. The equations of motion had to be integrated over a very long time, and it could take hours for a single run (still much faster than the Monte Carlo it was being used to do a sanity-check on). I re-wrote everything in C++, and picked up less th…

Also had the same experiences. Perl is relatively fast and memory compact compared to other modern scripting languages. Perl also has PDL ( http://pdl.perl.org/ ), which is very similar in scope to numpy, but predates it by several years.

predates it by several years

If you want to get technical, Numpy is a continuation of Numeric which in turn pre-dates pdl.

Post reply on HN