TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why Does Python Code Run Faster in a Function?

12 点作者 ScottWRobinson超过 1 年前

1 comment

raymondh超过 1 年前
Note that this effect varies across builds and versions. For most of Python&#x27;s history, we could give clear and invariant optimization advice (locals and nonlocals are fastest, global variable access was at least twice as slow, and builtin variable access was even slower). That ordering will likely remain true but absolute speeds have improved dramatically and the ratios have shifted).<p>Here is a run of Tools&#x2F;scripts&#x2F;var_access_benchmark.py for Python 3.12rc1 stock build for an Apple M1 Max (your mileage may vary):<p><pre><code> Variable and attribute read access: 1.9 ns read_local 2.4 ns read_nonlocal 2.8 ns read_global 4.1 ns read_builtin 5.0 ns read_classvar_from_class 12.1 ns read_classvar_from_instance 4.8 ns read_instancevar 4.7 ns read_instancevar_slots 12.2 ns read_namedtuple 29.0 ns read_boundmethod Variable and attribute write access: 2.4 ns write_local 2.5 ns write_nonlocal 10.5 ns write_global 26.8 ns write_classvar 4.3 ns write_instancevar 4.2 ns write_instancevar_slots Data structure read access: 5.7 ns read_list 11.1 ns read_deque 10.1 ns read_dict 10.5 ns read_strdict Data structure write access: 6.2 ns write_list 11.3 ns write_deque 11.2 ns write_dict 12.0 ns write_strdict Stack (or queue) operations: 18.4 ns list_append_pop 17.8 ns deque_append_pop 18.1 ns deque_append_popleft</code></pre>