4 ms·
>[boxed values...] The dynamic typing means that there are a lot more steps involved with any operation. This is a primary reason that Python is slow compared t
by wruza 9y ago
>[boxed values...] The dynamic typing means that there are a lot more steps involved with any operation. This is a primary reason that Python is slow compared to C for operations on numerical data.
Not exactly. Setting typecodes and vals doesn’t slow things down by many orders of magnitude. The main reason python is relatively slow is that there is no practical way to reason about what parts of program may be optimized out or leveled down to native datatypes and then restructured in a very efficient way. This is what optimizing/jit compilers do to achieve much performance; this one is the source of x100, not unboxing on its own. Technically, tracing jit that doesn’t care if you’re writing in static or dynamic, strict or duck typing can be done for any language, but (afaik) python is not very jit-friendly in general.
- ryanplant-au 9y agoDoes JavaScript make it easier to reason about those potentially-optimizable areas? Which language features make it easier to optimize to the level that V8 is? (V8 being 7-10x faster than CPython 3 on most of the Benchmarks Game.)
- acdha 9y agoThat’s just telling you that a JIT is faster than an interpreter. You’d want to compare PyPy to V8 to see how well they can both be JITed.
- bpicolo 9y agoIt's at least partially a factor of the cython implementation, because pypy does a pretty great job doing JIT compilation, and making python fast. That's without the resources of Google behind it, too. (Though I've seen some python compiler "20%" style projects roll around: https://github.com/google/grumpy https://github.com/google/grumpy). That said, python does have a lot of magic functionality that makes predicting paths through the program difficult for a compiler
- emodendroket 9y agoI feel like that's more a case of throwing resources at the problem until it improves than the language making life easier for optimizers.
- chubot 9y agoAs far as I understand, one important difference is that JavaScript doesn't have __getattr__ or __setattr__ (or at least earlier versions didn't). You might not use those hooks a lot in your application code, but it seems that web frameworks and the like do a lot of reflection, which makes the code difficult/impossible to optimize (even at runtime with a JIT). Python also has __add__ (operator overloading) and JavaScript doesn't. This is a good talk about how subtle or crufty the semantics of something like "a+b" is in Python: https://www.youtube.com/watch?v=IeSu_odkI5I https://www.youtube.com/watch?v=IeSu_odkI5I The PyPy developers had to copy a lot of the implementation details of CPython, which doesn't always result in the fastest code.
- mschaef 9y agoYou should take a look at the Chambers and Ungar papers on the implementation of Self. In Self, _everything_ is theoretically done through message send/dispatch. This includes field access, numerical operations, flow control... the works. (In a language with prototypical inheritance too.) What's interesting is that by the time they're done with their optimizations, they manage to get relatively close (x2, iirc) to native speed by essentially inlining everything they can and keeping enough metadata around that they can retain the dynamic properties of the languauge. Impressive stuff. (Which is probably why Sun hired them early in the Java/JIT days.)
- rurban 9y agoExactly. There are a lot of well-designed dynamically typed languages which beat the slow four (python, php, perl, ruby) by a large factor. You are talking about unboxing, but most fast dynamic languages rather have properly tagged primitives. Unboxing is not that performant as starting properly from scratch. For comparison e.g. php has now proper unboxing optimizations, perl6 also. But the properly tagged primitives such as in Chez scheme, Common Lisp, SELF, Javascript, lua or luajit (to cite the fastest) beat these by at least a factor of 10. Next problem is refcounting, as you can also easily observe with php7. Getting rid of most local refcounting madness led to a speedup of 100%. A proper GC leads to smaller data, faster updates and memory safety and leakfree-ness. Next false fact in this article: "Python is interpreted rather than compiled". Nope, cpython has a proper compiler. It translates the AST (a tree) to a linearized list with relative jumps and pretty tight ops and lot of compiler optimizations. Much better than the primitive ad-hoc AST interpreters of php or perl, which still carry around absolute ptrs all over in its ops. The problem with the python bytecode is that is still too big, compared to the fast bytecode interpreters. He probably meant "Python doesn't compile to native code, only bytecode". But there the speedup would be max. 2x, not 10-100 as observed with python. So this argument is bogus. 3. "Python's object model can lead to inefficient memory access" Yes, of course. There's no type optimization done. Which should be trivial and the first thing to do. But more importantly is the design of the hook system. E.g. let's compare it to Common Lisp, where you also can redefine any function/class/object or native data structure access on the fly or Javascript which is similar extremely dynamic. These systems have lots of optimizations for the fast general case, but python not. It generally turns down such optimizations.
- deleted 9y ago[deleted]
- acqq 9y ago> (afaik) python is not very jit-friendly in general. It's not too different from JavaScript, and as I've done some compiler development / run-time code generation myself, I really believe most of the tricks used in the modern JS jit implementations could significantly speed up the current Python. It would be probably too hard to make jits for all the platforms, of course, and it would increase the code base, and surely the memory demands, but for the most popular platforms it would be doable to reach JavaScript jit speeds for the code that does some calculations. That's what I hoped would have happened at the moment Google took Guido to work for them. But they obviously didn't have such goals. I think the biggest reason Python is still slow is that there weren't such kind of investments comparable to those for JS jits (or dot Net). On the purely technical level, I really believe it would be very possible. At least for a single-thread execution, and for the heavy calculations, disregarding the famous GIL or some other aspects.
- mschaef 9y ago> I think the biggest reason Python is still slow is that there weren't such kind of investments comparable to those for JS jits (or dot Net). I think this is spot on. Python, Ruby, and the JVM all originated in the 1990-95 timeframe, and all started out with while-loop/switch statement bytecode interpreters. Java had the commercial backing of Sun, who viewed it as a way to compete with Microsoft Windows and invested a lot of money into making Java faster. (HotSpot, etc.) As far as I know, lacking that kind of investment, the main Python and Ruby implementations are still interpreting bytecode 25 years later. .Net was a little bit different because by the time it came around, JIT was widely understood to be a useful thing, and Microsoft built the CLR with JIT as one of the founding principles. I also tend to believe that Emacs suffers the same sorts of problem. At it's core, Emacs is a Lisp implementation, and high-performance concurrent language implementations are expensive to build.
- acqq 9y agoYes, some maybe forget, others were too young, but Firefox JavaScript implementation was actually unbelievably, awfully slow for any longer calculations before Google financed and published V8 (2008), e.g. in this benchmark from 2007: https://www.fourmilab.ch/fourmilog/archives/2007-09/000896.html https://www.fourmilab.ch/fourmilog/archives/2007-09/000896.h... The attitude before the public V8 outside of Google was "it's good enough as it is and it would be too hard to do anything about it." That JS was even slower than Ruby, which was some 30 times slower than C, at that time. Now the jitted JavaScript can run at least at about the speed of the non-optimized C, when doing calculations. Python is not so slow as that particular JS was then, but I see no technical reason that the speeds comparable to the current JS jits couldn't be reached.
- katastic 9y ago>Setting typecodes and vals doesn’t slow things down by many orders of magnitude. What about blowing the cache by having every access involve multiple pointer dereferences? Virtual calls are absolutely slower than normal calls. But yeah, 100x is not likely from virtual calls unless you're forcing a cache invalidation on every access. Running a CPU without cache brings you down 100x-200x (literal) in performance on most modern machines. (The Cell CPU was almost exactly 200, which is why Sony did TONS of PR and tech talks on Data-oriented Design because the Cell would be useless without cache aware programming.) But on further reflection, you said "this is one source of 100x, not unboxing on its own" so we're more in agreement than disagreement I guess. My point is that virtual calls and dereferences can definitely be a huge performance hit if the cache/TLB can't keep up with it.
- gravypod 9y ago> but (afaik) python is not very jit-friendly in general. It's time for the time honored "Sufficiently Smart Compiler" [0] argument. All programming languages can be optimized in meaningful ways via inferred information. There is a small subset of python programs that I don't see as possible to optimize at all. These programs would be ones using the Pickle library who load code and execute functions from that code. So long as your code is not doing any Evil/Eval() all performance critical portions of your code should theoretically be inferrable. Note: I said "should be inferrable" NOT "should be easily inferrable". This won't be easy and it takes teams of people to do this on a subset of python's feature set (see Numba). [0] - http://wiki.c2.com/?SufficientlySmartCompiler http://wiki.c2.com/?SufficientlySmartCompiler
- valarauca1 9y agoI’m just gonna jump in. The JVM has no problem hot loading, unloading, or redefining presently executed code. What I’m trying to say is JIT’ing dynamically loaded code isn’t rocket science
- gravypod 9y agoIt's much more complicated and a much lower on the cost-benefit totem-pole than... 1. Inlining 2. Compiling bytecode for a specific set of input and output types 3. Object Recycling 4. Runtime-compiling to machine code Things like that. edit: I may be wrong. I'd love to see an example of a VM that implements what you're thinking.
- dr_zoidberg 9y agoWhile the name of the talk implies it's a generally-speaking-talk, Click goes into a lot of detail of how the JVM works in his "A Crash Course in Modern Hardware" presentation[0]. I saw it a while ago, but I think he does mention those things. Of course that's the one I saw and remember, but you could probably find another presentation in which he goes into much better detail about the JVM and its inner workings. [0] https://www.youtube.com/watch?v=OFgxAFdxYAQ https://www.youtube.com/watch?v=OFgxAFdxYAQ