
The Performance Impact of Using dict() Instead of {} in CPython 2.7 - spdy
http://www.doughellmann.com/articles/misc/dict-performance/
======
rlpb

        > [tl;dr] Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.
        [...]
        > ...as a general principle I try to avoid code constructions I know to introduce performance hits.
    

This is not Pythonic. I appreciate the article and what it showed me, but I do
not agree with the conclusion. Do whatever makes your code most readable.
Sometimes this may be {}, and sometimes it may be dict(). Most code will not
be in a performance-critical path.

"Premature optimization is the root of all evil".

------
andreasvc
While it's nice to get a peek at the implementation details of CPython in this
way, I doubt that dict(foo=bar, ...) is typically going to be part of a
critical inner loop. It's much more likely to be executed just once before a
loop, making this finding irrelevant for real optimization problems.

~~~
haeric
What are you using Python for? There are plenty of occations where I have made
lists of dictionaries, e.g. when I have have large amounts of data that have
to be outputted in a certain way, or if it's just logical to store
dictionaries instead of instances of some custom class. No performance
critical stuff though, and I've almost never used dict(), but still, calling
this irrelevant for optimization is a bit harsh...

------
lhnz

        {k: v for k, v in some_iterable}
    

I'd never seen that. Nice. :)

~~~
w0utert
Dictionary comprehensions are pretty nice indeed, they're new in Python 2.7 so
that might be why you've not seen them before ;-)

------
rplnt
If you don't have the time to read through just jump here:
[http://www.doughellmann.com/articles/misc/dict-
performance/#...](http://www.doughellmann.com/articles/misc/dict-
performance/#conclusions)

------
brandoncarl
Doug - thank you for this piece. It clearly took a lot of time to
research/write and in addition to providing an interesting insight, provider a
great look into disassembling and interpreters.

I would hope that over time insights like this combine with others to create a
set of "best practices" across a language. After all, readability is largely
tied to convention (multi-line ternary if statements excluded!).

The real benefit of these performance tweaks comes in those people writing
libraries. After all, most of the matrix routines we depend on today still tie
back to FORTRAN - amazing we are using 40 year old code!

Thanks again.

------
codeape
From the article:

    
    
        I’ve been reviewing lot of code lately for various open 
        source and internal projects written in Python. As part of 
        those reviews, I have noticed what I think is a trend 
        toward using dict() instead of {} to create dictionaries. I 
        don’t know exactly why this trend has emerged.
    

My guess for why this trend has emerged: People prefer javascripts object
literal syntax where the attribute names are not quoted.

After writing javascript:

var o = { a: 1, b: 2};

... it feels a bit clunky to write Python:

o = { "a": 1, "b": 2}

~~~
dbecker
How does using dict() solve the requirement for quotes that you've identified?

~~~
ashika
dict(foo='bar') becomes {'foo': 'bar'}, removing the need for quotes around
the key.

------
ra
PyCharm really irks me when it gives a warning when I declare an empty
dictionary using a literal {} rather than the dict() global function.

It does the same thing with [] and list().

I'd rather just use what feels right at the time thanks, PyCharm.

~~~
Flow
Can't you just alter the settings for those code-warnings?

~~~
ra
Oh wow you'e totally right :-)

It's under, "introspections" if anyone else is looking.

