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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Performance Analysis of Python's Dict() and {}

108 点作者 nathiss超过 1 年前

12 条评论

trostaft超过 1 年前
Unless I&#x27;ve misunderstood the comparison, the lists example does not appear to be comparing apples to apples. Reproducing here:<p><pre><code> $ python -m timeit &quot;list((1, 2, &#x27;a&#x27;))&quot; 5000000 loops, best of 5: 53 nsec per loop $ python -m timeit &quot;[(1, 2, &#x27;a&#x27;)]&quot; 10000000 loops, best of 5: 30.4 nsec per loop </code></pre> The first command produces a list of three elements, whereas the second produces a list of one element (being the tuple).
评论 #39095205 未加载
sweezyjeezy超过 1 年前
I don&#x27;t think this will refute the article, but I would have found it more convincing if the benchmark had included a single setitem assignment as well, to be sure that the difference wasn&#x27;t python doing a lazy dict-or-set type assignment on {}
评论 #39096902 未加载
ezo超过 1 年前
I’m wonder, who’s thinking that dict() is more readable???
评论 #39095083 未加载
评论 #39094991 未加载
评论 #39096005 未加载
评论 #39095254 未加载
评论 #39095032 未加载
评论 #39095336 未加载
评论 #39095008 未加载
评论 #39095700 未加载
评论 #39095080 未加载
Cannabat超过 1 年前
If you want to implement the suggested changes across your codebase, you can use the `flake8` plugin `flake8-comprehensions`, or `ruff`&#x27;s `C4` ruleset, which is an implementation of `flake8-comprehensions`.
justinl33超过 1 年前
Best way to optimize a python dict is to switch languages and use a dict there
评论 #39097074 未加载
bigbillheck超过 1 年前
The difference is 20ns, and if that&#x27;s enough for you to care about you&#x27;ve got much bigger worries (like &quot;using python&quot;).
评论 #39094929 未加载
评论 #39094957 未加载
评论 #39095609 未加载
评论 #39094904 未加载
评论 #39095091 未加载
wyldfire超过 1 年前
A previous analysis of the same question (2.7, 2012): <a href="https:&#x2F;&#x2F;doughellmann.com&#x2F;posts&#x2F;the-performance-impact-of-using-dict-instead-of-in-cpython-2-7-2&#x2F;" rel="nofollow">https:&#x2F;&#x2F;doughellmann.com&#x2F;posts&#x2F;the-performance-impact-of-usi...</a>
mgaunard超过 1 年前
does your colleague also uses list instead of []?<p>If you&#x27;re in Python, you should embrace the syntax.
pphysch超过 1 年前
The first &quot;benchmark&quot; shows dict() being twice as fast, but the rest of the article concludes &quot;The {} is always faster than dict.&quot;
评论 #39094819 未加载
评论 #39094800 未加载
评论 #39094839 未加载
ayhanfuat超过 1 年前
From Alex Martelli , a prominent Python expert [1]:<p>&gt; I&#x27;m one of those who prefers words to punctuation -- it&#x27;s one of the reasons I&#x27;ve picked Python over Perl, for example. &quot;Life is better without braces&quot; (an old Python motto which went on a T-shirt with a cartoon of a smiling teenager;-), after all (originally intended to refer to braces vs indentation for grouping, of course, but, hey, braces are braces!-).<p>&gt; &quot;Paying&quot; some nanoseconds (for the purpose of using a clear, readable short word instead of braces, brackets and whatnots) is generally affordable (it&#x27;s mostly the cost of lookups into the built-ins&#x27; namespace, a price you pay every time you use a built-in type or function, and you can mildly optimize it back by hoisting some lookups out of loops).<p>&gt; So, I&#x27;m generally the one who likes to write dict() for {}, list(L) in lieu of L[:] as well as list() for [], tuple() for (), and so on -- just a general style preference for pronounceable code. When I work on an existing codebase that uses a different style, or when my teammates in a new project have strong preferences the other way, I can accept that, of course (not without attempting a little evangelizing in the case of the teammates, though;-).<p>[1] <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;2745292&#x2F;2285236" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;2745292&#x2F;2285236</a>
评论 #39095743 未加载
评论 #39095774 未加载
评论 #39096530 未加载
t8sr超过 1 年前
While it points at interesting questions about Python&#x27;s internals, I hope people writing Python realize that optimizing it is pointless, except for cases where you change the complexity class of an algorithm.<p>The performance of pure python code is orders of magnitude worse than non-interpreted languages, there&#x27;s no point trying to shave off 0.5% off a 5000% difference.
评论 #39095522 未加载
评论 #39096236 未加载
评论 #39095932 未加载
评论 #39095692 未加载
评论 #39095993 未加载
评论 #39095452 未加载
评论 #39095740 未加载
评论 #39096535 未加载
__loam超过 1 年前
Python is filled with these kinds of traps where there&#x27;s more than one way to do the same thing but one way is faster for no reason. It&#x27;s such a bad language.
评论 #39097346 未加载
评论 #39096649 未加载