7 ms·
Recent Performance Improvements in Function Calls in CPython
- winrid 2y agoAFAIK this is because CPython has to walk the scope up to find the import for every call in your loop, and still applies to python3, right? You can still use the built in min, just create a "closer" reference before your loop for the same speedup: inline_min = min while expr: if inline_min(blah):
- boxed 2y agoThat's not how imports work in python at all. Imports are super imperative operations that first tries to find it in sys.modules, otherwise executes the module and puts the resulting dict in sys.modules. Then it grabs the symbols you asked for and shoves all those symbols into the global dict for the module. It does have to walk the locals and the globals scopes (there are ONLY exactly two scopes in Python!) to find the function, that's true.
- winrid 2y ago> It does have to walk the locals and the globals scopes right, this is what I meant.
- winrid 2y agoAlso I imagine the dict lookup for the module is the slow part? So declaring in local scope just removes a dict lookup? I am by no means a python expert :) I just use it occasionally.
- dfox 2y ago15+ years ago many people would tell you that you should do “from foo import bar” instead of “import foo” to save the overhead inherent in referencing “bar” as “foo.bar”. Well, as almost everything in Python is in the end an dict, the dict implementation is ridiculously optimized (and in some counter-intuitive ways), so if you care about that overhead you probably should not be using (C)Python in the first place. This “everything is a dict” is also a part of why removing GIL is not as straightforward as it would seem.
- winrid 2y agoActually even on a modern CPU that property lookup does add another ~10% penalty. Granted it's like a .1 second difference out of a second runtime for 10m iterations, but it's measurable! Although I wouldn't be using python on those data sizes usually :)
- hansvm 2y agoTIL. Apparently `nonlocal` and closures are implemented with copies, at least for my copy of dis.dis().
- ipsum2 2y agoBenchmark it, it won't be any faster. Using the if statement: Execution time: 0.00648 seconds Using the min function directly: Execution time: 0.02298 seconds Using the min function with an intermediate variable: Execution time: 0.02959 seconds
- necovek 2y agoHow about using the `min` builtin directly over the entire list?
- winrid 2y agoSure, let's benchmark it :) It is consistently around 8% to 15% faster on 3.10.12 and 3.11 for me. On 3.12.5 (latest) I seem to get the same result. https://www.online-python.com/B6AgKW5zod https://www.online-python.com/B6AgKW5zod (please copy the code to your local to not ddos this site :D)
- otteromkram 2y agoI agree with you. It's a weird quirk of Python where localization is more efficient.
- b3orn 2y agoIt makes a slight difference on my system, python 3.12.0 In [1]: def test_1(): ...: i = 10_000_000 ...: r = 0 ...: ...: while i>0: ...: i = i-1 ...: r += min(i, 500) ...: ...: return r In [2]: def test_2(): ...: i = 10_000_000 ...: r = 0 ...: local_min = min ...: ...: while i>0: ...: i = i-1 ...: r += local_min(i, 500) ...: ...: return r In [3]: %timeit -n 1 -r 10 test_1() 2.14 s ± 58.4 ms per loop (mean ± std. dev. of 10 runs, 1 loop each) In [4]: %timeit -n 1 -r 10 test_2() 1.94 s ± 47.8 ms per loop (mean ± std. dev. of 10 runs, 1 loop each)
- akdhaqwdj 2y ago[flagged]
- necovek 2y agoI wonder how would simply doing a `return min(heights)` compare to any of the options given? (It sure doesn't demonstrate the improvements between interpreter versions, but that's the classic, Python way of optimizing: let builtins do all the looping)
- necovek 2y agoI was curious myself, so I've ran a quick benchmark: import random import timeit heights = [random.randint(0, 10000)/100 for i in range(10000)] def benchmark1(heights): smallest = heights[0] count = len(heights) - 1 while count > 0: if heights[count] < smallest: smallest = heights[count] count -= 1 return smallest def benchmark1b(heights): a = 1 b = len(heights) - 1 min_height = heights[0] while a < b: if heights[a] < min_height: min_height = heights[a] a += 1 return min_height def benchmark2(heights): smallest = heights[0] count = len(heights) - 1 while count > 0: smallest = min(heights[count], smallest) count -= 1 return smallest print(timeit.timeit('min(heights)', number=1000, globals={'heights': heights})) print(timeit.timeit('benchmark1(heights)', number=1000, globals={'heights': heights, 'benchmark1': benchmark1})) print(timeit.timeit('benchmark1b(heights)', number=1000, globals={'heights': heights, 'benchmark1b': benchmark1b})) print(timeit.timeit('benchmark2(heights)', number=1000, globals={'heights': heights, 'benchmark2': benchmark2})) Here are the results in Python 3.11: 0.04471710091456771 0.21777329698670655 0.22779683792032301 0.6679719020612538 So, using min over a list is ~5x faster, using a single variable and a constant 0 is ~5% faster than using two for boundaries, and using min inside the loop instead of the if check is another 3 times slower: so, the old approach of looking for opportunities to use a builtin instead of looping still likely "wins" in the newer interpreters too, but if someone's got 3.14 alpha up, I'd love to see the results. I might install 3.13 to check it out there too.
- otteromkram 2y agoNow try with a for loop.
- 2y ago
- L-four 2y agoLoops should be avoided in python. Only constant time operations should be performed.
- kaldah 2y ago[flagged]
- hoten 2y agoSo there's only three super-instructions? I wonder if there are plans for more.
- Ralfp 2y agoThey removed most of superinstructions because those prevented other more effective optimizations: https://github.com/python/cpython/issues/105229 https://github.com/python/cpython/issues/105229
- otteromkram 2y agoI don't trust anyone who uses camelCase to write Python. Or, unnecessary while loops. But, sometimes you can improve built in functions. I found that using a custom (but simple) string-to-int function in golang is a bit quicker than strconv.formatInt() for decimal numbers. So, there's that. Python isn't really supposed to be geared towards performance. I like the language, but only see articles like this as resume fodder.
- lacksconfidence 2y ago> but only see articles like this as resume fodder. Not super important, but i think it's worth calling out. Some of us program because it's fun. We dig into problems like this because it scratches an itch. We share in the hopes of interacting with like-minded individuals. Not everything is about resumes.
- mananaysiempre 2y agoPersonally I tend to discount the possibility of such motivations for a text once that text is interrupted in the middle to sell me something (which a Substack form counts as).
- sgarland 2y agoIndeed. Some of us do it because we stubbornly want to see just how fast we can make a “slow” language go.
- hoten 2y agoWow, very unfair. It should not be surprising that people writing Python might actually care about performance, even if it isn't so important that they must write their software in assembly. The code wasn't the authors, and what's more it's just an example to talk about the bytecode. Everyone knows `min(some_list)` is faster and more Pythonic. This is a very interesting article and probably exposed a few people to a few interesting Python internals.
- otteromkram 2y ago
- maccard 2y agoI'm always shocked at how much performance we leave on the table in these places. I did a super quick benchmark in python[0] and go [1] (based on necovek's[2] comment) and ran them both locally. The go implementation runs the entire benchmark quicker than the fastest of the python ones. The deviation in go's performance is still large, but far less so than Python's. Making the "wrong" choice for a single function call (bearing in mind that this is 10k iterations so we're still in the realm of scales even a moderate app can hit) in python is catastrophic, making the wrong choice for go is a significant slowdown but still 5x faster than doing it in Python. That sort of mental overhead is going to be everywhere, and it certainly doesn't encourage me to want to use python for a project. [0] https://www.online-python.com/9gcpKLe458 https://www.online-python.com/9gcpKLe458 [1] https://go.dev/play/p/zYKE0oZMFF4?v=goprev https://go.dev/play/p/zYKE0oZMFF4?v=goprev [2] https://news.ycombinator.com/item?id=41196915 https://news.ycombinator.com/item?id=41196915
- serjester 2y agoCpython deliberately chooses to prioritize simplicity, stability and easy C extensions over raw speed. If you run the benchmark in PyPy (the performance optimized implementation) it’d be 10X faster. You could argue anything that’s performance bottlenecked shouldn’t be implemented in Python in the first place and therefore C compatibility is critical.
- maccard 2y agoI don't have easy access to pypy so I can't run the benchmark on that I'm afraid. I'd love to see some relative results though. > You could argue anything that’s performance bottlenecked shouldn’t be implemented in Python in the first place and therefore C compatibility is critical. Honestly, I think I'm heading towards "python should only be used for glue where performance does not matter". The sheer amount of time lost to waiting for slow runtimes is often brought up with node and electron, but I ran the benchmark in node and it's about halfway between go and python.
- igouy 2y ago
- pansa2 2y agoOne of the performance improvements mentioned is "Remove the usage of the C stack in Python to Python calls" [0]. Since Python 3.11, a Python-level function call can be evaluated within the bytecode interpreter loop, no longer requiring a C-level function call. Interestingly, Lua 5.4 did the opposite. Its implementation introduced C-level function calls for performance reasons [1] (although this change was reverted in 5.4.2 [2]). [0] https://bugs.python.org/issue45256 https://bugs.python.org/issue45256 [1] https://github.com/lua/lua/commit/196c87c9cecfacf978f37de4ec69eba0a5971256 https://github.com/lua/lua/commit/196c87c9cecfacf978f37de4ec... [1] https://github.com/lua/lua/commit/5d8ce05b3f6fad79e37ed21c1076e47a322472c6 https://github.com/lua/lua/commit/5d8ce05b3f6fad79e37ed21c10...
- alberth 2y agoDo you by chance know what LuaJIT does?
- mananaysiempre 2y agoIn JITted code? Nothing, it’s a pure tracing JIT so all calls are flattened. In the interpreter? We can always check the source[1], and it seems it implements its own stack. [1] https://repo.or.cz/luajit-2.0.git/blob/HEAD:/src/vm_x64.dasc https://repo.or.cz/luajit-2.0.git/blob/HEAD:/src/vm_x64.dasc: look for BC_CALL[2], then ins_call before that, then ins_callt just above it. [2] see mirror of the old wiki for the semantics: https://chrisfls.github.io/luajit-wiki/Bytecode/#Calls-and-Vararg-Handling https://chrisfls.github.io/luajit-wiki/Bytecode/#Calls-and-V...
- bongodongobob 2y agoCan you explain what this means? I know all these words, but it looks like retro-encabulator to me.
- pansa2 2y agoWhen one Python function calls another, does that ultimately result in the CPU executing a `call` instruction, or not? Python 3.11 changed from "yes" to "no" for performance reasons. Lua, OTOH, changed from "no" to "yes" in 5.4.0, again for performance. It then changed back to "no" in 5.4.2 for other reasons, despite the performance regression. (In Lua 5.1, instead of call/`return`, `coroutine.resume/yield` is implemented using C-level call/`return`. That would preclude implementing call/`return` the same way - but since 5.2 the coroutine functions are instead implemented via call/`longjmp`.)
- Waterluvian 2y agoI think the safest way to understand Python is that performance is a “nice to have” feature, traded off for legibility and flexibility and even simplicity of the CPython code base (go read it! You barely need to understand C and a lot of it is written in Python!). Don’t waste time being surprised that you can do better than the default implementation. Just assume that and do so when you’ve measured that it matters. That being said, (mostly) free performance is always nice. I’m glad they’re working on performance improvements where they can do it without sacrificing much.
- alberth 2y agoTake a look at Nim. You get C performance, with the readability of Python. https://nim-lang.org/ https://nim-lang.org/
- dilawar 2y agoI really really want nim to catch up. It's a lovely language. I've used it for small projects and it is great but I couldn't use it for anything complex that requires third-party libraries. The ecosystem is just not there. I wonder what is stopping community mindshare?
- stoperaticless 2y agoI bet same reason that you just outlined. (Chicken and the egg problem)
- Waterluvian 2y agoPretty much. When Python was at this stage, what alternatives did a developer have than to improve Python/write a library? Nim has to “compete” with a solution already being available.
- alberth 2y agoPerl dominated before Python, and had a massive library of modules via CPAN. Clearly Python eclipsed Perl. Just say'n.
- mg 2y agoReading this, I was interested to see how big the speed difference between Python and PHP is these days, so I tried it like this: min.py: i = 10_000_000 r = 0 while i>0: i = i-1 r += min(i,500) print(r) min.php: <?php $i = 10_000_000; $r = 0; while ($i>0) { $i = $i-1; $r += min($i,500); } print($r); The results: time python3 min.py 4999874750 real 0m2.523s time php min.php 4999874750 real 0m0.333s Looks like Python is still 8x slower than PHP. Pretty significant. I ran it with Python 3.11.2 and PHP 8.2.18
- winrid 2y agoThe dynamic aspect of Python is once again probably the major penalty here. One major difference here is that in Python you can overwrite min(), so it has to access this function through more abstraction, but not in PHP. I bet PyPy is close to PHP here however.
- JohnKemeny 2y agoWith pypy, the code runs in 0.226s on my computer. So yes, the comparison of OP is CPython vs PHP.
- mg 2y agoYes, it is CPython. Added data on the PyPy performance to this comment now: https://news.ycombinator.com/item?id=41199431 https://news.ycombinator.com/item?id=41199431
- mg 2y agoYou might be right about the min() call being the major penalty. When I avoid the min() call like this ... min2.py: i = 10_000_000 r = 0 while i>0: i = i-1 if i<500: r += i else : r += 500 print(r) I get a speedup of about 50%: time python3 min2.py 4999874750 real 0m1.338s Still 4x slower than PHP though. With PyPy I get an amazing speedup of 96%: time pypy3 min.py 4999874750 real 0m0.088s 4 times faster than the PHP version! I'm not sure what the state of things is regarding PyPy and running a web application. Doing a bit of googling, it seems that calling C functions from PyPy is slow, so one would have to check if the application uses those (for example to connect to the database).
- physicsguy 2y agoHere we go, another HN roundabout criticism of Python performance. Here’s a real world example. I recently did some work implementing DSP data pipeline. We have a lot of code in Go, which I like generally. I looked at the library ecosystem in Go and there is no sensible set of standard filtering functions in any one library. I needed all the standards for my use case - Butterworth, Chebyshev, etc. and what I found was that they were all over the place, some libraries had one or another, but none had everything and they all had different interfaces. So I could have done it in Go, or I could have kept that part in Python and used SciPy. To me that’s an obvious choice because I and the business care more about getting something finished and working in a reasonable time, and in any case all the numeric work is in C anyway. In a couple of years, maybe that ecosystem for DSP will be better in Go, but right now it’s just not ready. This is the case with most of our algorithm/ML work. The orchestration ends up being in Go but almost everything scientific ends up in Python as the ecosystem is much much more mature.
- klabdn 2y agoThat is fair enough, but using Python as glue with existing C libraries is the acknowledged use case for Python. I think it is even admitted in this thread. But for that use case the claimed performance improvements in pure Python (which I can rarely replicate) are not relevant. They could leave the interpreter as it was for +20 years. For web applications, migrating to Go or PHP should not require much thought apart from overcoming one's inertia. Hopefully the scientific stack will be rewritten in better languages like Go, Lisp, Ocaml, etc.
- bayindirh 2y ago> Hopefully the scientific stack will be rewritten in better languages like Go, Lisp, Ocaml, etc. I don't keep my breath on that. Because Python can be used interactively, relatively mature, hard plumbing work is done by real computer scientists, and all is left to play with the code until the numbers look right for a given research. It's just a better MATLAB in practice for some disciplines, and none of the languages we have provides this kind of flexibility with that amount of leeway for play. Zip the virtualenv and send over. If it doesn't work just delete the folder and reinstall anaconda... Nobody is thinking about the performance of their code or the elegance of what they do. They care about the papers, deadlines and research in general. Code quality, general performance, organization can go whereever they like. All underlying high performance code is written in C anyway and only implementers of these code cares about that code. It's akin to docker for (data) scientists mostly. How do I know? I'm an HPC admin. I see that first hand.
- Bostonian 2y agoOn the general topic of Python performance, just importing needed modules can take a few seconds. The output of the code import time start = time.time() import os import numpy as np import pandas as pd import xgboost as xgb from sklearn.metrics import mean_squared_error from scipy.stats import pearsonr import matplotlib.pyplot as plt print("time elapsed (s):", "%0.3f"%(time.time() - start)) on my Windows machine is time elapsed (s): 2.630
- nitely 2y agoIf those libraries are doing something at import time, then it could take any amount of time, TBF.
- noitpmeder 2y agoNot sure why this matters too too much, this is a one-time setup cost (of some significantly large libraries). Python doesn't import everything in your virtual environment at startup, which I think we can agree is the right choice. I'd further _guess_ that only one or two of those libraries are the actual slowdowns. See here for how to tell: https://stackoverflow.com/a/51300944/602469 https://stackoverflow.com/a/51300944/602469
- cryptonector 2y agoIt matters when you have lots of short-lived processes.
- incorrecthorse 2y agoIt can take an arbitrary amount of time. Modules are just code executed top to bottom, and might contain anything beyond mere constants and functions declaration.
- deleted 2y ago[deleted]
- rbanffy 2y agoI’ll just leave this here: https://paulgraham.com/avg.html https://paulgraham.com/avg.html
- svilen_dobrev 2y agofew months ago i had to optimize some old python code, .. and found that my longtime assumptions from py 1,2 and early 3.x are not anymore true. So put up some comparisons.. Check them here: https://github.com/svilendobrev/transit-python3/blob/master/benchmark/timing-probi.py https://github.com/svilendobrev/transit-python3/blob/master/... (comment out the import transit.* and the two checks after it as they are specific. Takes ~25 seconds to finish) Results like below. Most make sense, after thinking deeper about it, but some are weird. One thing stays axiomatic though: no way of doing something is faster than not doing it at all. Lesson: measure before assuming anything "from-not-so-fresh-experience". btw, unrelated, probably will be looking for work next month. Have fun. $ python timing-probi.py ... :::: c_pfx_check :::: f_tuple 0.10805996900126047 f_list 0.10568888399939169 f_tuple_global 0.10741564899944933 f_list_global 0.10980218799886643 f_dict_global 0.09630626599937386 f_tuple_global20 0.6103107449998788 f_list_global20 0.6878404369999771 f_one_by_one 0.05088467200039304 :::: c_func_glob_vs_staticmethod :::: f_glob_func 0.08005491699987033 f_staticmethd 0.10022392999962904 :::: c_for_loop_vs_gen_for :::: for_loop 0.2255296620005538 gen_loop 0.29973782500019297 :::: c_dictget_vs_dict_subscr :::: dictgetattr_get 0.05093873599980725 dictfuncget 0.048424991000501905 dictin_dictsubscr 0.04722780499832879 dictsubscr 0.04069488099958107 :::: c_listget_vs_list_subscr :::: listgetattr_get 0.0779018819994235 listfuncget 0.07271830799982126 listsubscr 0.057812218999970355 :::: c_property_vs_funccall :::: property 0.08194994600125938 funccall 0.08422214100028214 :::: c_a_in_abc_vs_a_eq_b_or :::: x_in_str 0.04265176899934886 x_in_tuple 0.0530087259994616 x_in_tuple_global 0.05479079300130252 x_eq_a_or 0.049807468998551485 :::: c_tuple_vs_slots :::: slots 0.17708088200015482 plain 0.18551878399921407 tuple 0.07675717399979476 dict 0.14878148099887767 namedtuple 0.2637523979992693 dataclass 0.18731526199917425 dataclassfrozen 0.38634534500124573 :::: c_dictcomp_vs_dict_gen_tuples_vs_loop :::: dictcomp 4.476536423999278 dict_gen_tuples 7.045798945999195 dict_listcomp 6.461099333000675 dictloop 4.889642943000581 :::: c_listcomp_vs_list_gen_vs_loop :::: listcomp 1.4269603859993367 list_gen 2.6340354429994477 listloop1 1.856299590001072 listloop2 2.1041324060006446 :::: c_funccall_args_vs_kargs :::: args 0.14374798999961058 kargs 0.1689707850000559 kargs_ignored 0.2658924890001799 kargs_default 0.26562809300048684