4 ms·
Everyone knows that Python's performance is generally pretty bad, but that seems almost comical to me. How can it take more than 400 cycles just to do a procedu
by userbinator 12y ago
Everyone knows that Python's performance is generally pretty bad, but that seems almost comical to me. How can it take more than 400 cycles just to do a procedure call and return?
There's all the instructions in the interpreter's main loop (which probably contains a bunch of call/returns on its own...), plus the cycles needed to read the instructions it's interpreting (including the manual management of the VM's call stack), then throw in a cache miss or two, and 400 cycles starts looking pretty good for an interpreted language.
I'm of the opinion that if call/return time is taking up a significant portion of the total running time of a program, then the problem is with the program's structure and nothing is going to solve it better than fixing the code itself (e.g. don't break it up into so many little pieces that most of the functions don't do much more than call others; but don't inline too much, since that'll increase cache misses.) All the clever trickery with stack/heap allocation to bring down the overhead is interesting, but it can't ever beat the zero cycles of not doing a call at all. As the saying goes, "the fastest way to do something is to not do it at all."
- srean 12y ago> then the problem is with the program's structure ....or with the choice of the programming language. Some styles goes better together with some programming languages. CPython is possibly the worst offender in terms of function call overhead, amplified further by its lack of inlining / macro capabilities. PyPy makes it better, but were they ever fighting an uphill task ! Python, on retrospect, seems to have been designed almost as a snarky challenge thrown at someone: "You think you are too smart, do you ? Let me see how you can make this go fast. No I want elide tail calls".
- lambdanator 12y agoWhy would inlining too much increase cache misses? I've never heard that.
- hawkice 12y agoAnything that increases executable size might/will cause it to make more fetches into memory, if only to get all those bonus instructions. I'd say this is a pretty minor effect in every scenario I've dealt with. Haskell, in particular, lets you inline very easily (and so many people do if performance is important), and I've only heard of one time where that had a negative consequence, and that was mostly about a missing optimization that you had to fix in a non-removing-inlining kind of way (it had to do with polymorphic strings leading to code inlined at every single string literal, which is extremely silly).
- TheLoneWolfling 12y agoYep. Note, however, that, as a special case, inlining of a function only called in one place "should" always be faster.