Another reason python is slower than other languages is that abstraction adds significant overhead which most implementations cannot optimize away. See here: http://blog.reverberate.org/2014/10/the-overhead-of-abstract...
The overhead of abstraction...unless you're using Haskell where typeclass instances get specialized, so you can abstract without slowing things down.
Why Python Is Slow: Looking Under the Hood
31–40 of 156 posts
Re: Why Python Is Slow: Looking Under the Hood
#32Re: Why Python Is Slow: Looking Under the Hood
#33Re: Why Python Is Slow: Looking Under the Hood
#34Earlier 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…
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 produce a parse tree prior to running the program.I could not get your code sample to die based on the type/value of "whatever" -- can you?
Re: Why Python Is Slow: Looking Under the Hood
#35A 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…
Perl also has PDL (http://pdl.perl.org/), which is very similar in scope to numpy, but predates it by several years.
Re: Why Python Is Slow: Looking Under the Hood
#36Earlier quoted context omitted.
When the GP talks about "small integers", I'm assuming he's talking about numbers between -5 and 256, which are cached ahead of time. They're never going to be garbage collected, so refcounting is largely unnecessary.
Once again, you can't implement this without specializing every call site that invokes Py_INCREF or Py_DECREF, or in other words, "1 conditional branch per object access everywhere."
This is essentially why a JIT can be almost as fast as compiled code even when including the necessary guards. It's only when the branches become unpredictable that the costs become real. The trick with fast interpreted code is threading the execution so that the hardware is able to predict the branches accurately, but this is largely independent of the number of conditional branches.
Re: Why Python Is Slow: Looking Under the Hood
#37A 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…
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/benchmark.php?te...
Re: Why Python Is Slow: Looking Under the Hood
#38Earlier 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…
Because CPython implements its virtual machine with an interpreter, without the option of, say, a JIT compiler.
Re: Why Python Is Slow: Looking Under the Hood
#39Dynamic 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 called with. Effectively emitting specialized versions of specific nodes in the control flow graph.