News for hackers. Stuff that matters.
Re:The Performance Impact of Using dict() Instead of {} in C... by rlpb on Wednesday November 14, 2012 @06:09AM (#4782889)

    > [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".

Re:The Performance Impact of Using dict() Instead of {} in C... by andreasvc on Wednesday November 14, 2012 @05:50AM (#4782847)
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.
Re:The Performance Impact of Using dict() Instead of {} in C... by haeric on Wednesday November 14, 2012 @08:46AM (#4783248)
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...
Re:The Performance Impact of Using dict() Instead of {} in C... by lhnz on Wednesday November 14, 2012 @04:47AM (#4782705)

    {k: v for k, v in some_iterable}
I'd never seen that. Nice. :)
Re:The Performance Impact of Using dict() Instead of {} in C... by w0utert on Wednesday November 14, 2012 @05:05AM (#4782746)
Dictionary comprehensions are pretty nice indeed, they're new in Python 2.7 so that might be why you've not seen them before ;-)
Re:The Performance Impact of Using dict() Instead of {} in C... by rplnt on Wednesday November 14, 2012 @04:04AM (#4782593)
If you don't have the time to read through just jump here: http://www.doughellmann.com/articles/misc/dict-performance/#...
Re:The Performance Impact of Using dict() Instead of {} in C... by brandoncarl on Wednesday November 14, 2012 @08:43AM (#4783224)
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.

Re:The Performance Impact of Using dict() Instead of {} in C... by codeape on Wednesday November 14, 2012 @08:35AM (#4783201)
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}

Re:The Performance Impact of Using dict() Instead of {} in C... by dbecker on Wednesday November 14, 2012 @09:43AM (#4783498)
How does using dict() solve the requirement for quotes that you've identified?
Re:The Performance Impact of Using dict() Instead of {} in C... by ashika on Wednesday November 14, 2012 @10:19AM (#4783663)
dict(foo='bar') becomes {'foo': 'bar'}, removing the need for quotes around the key.
Re:The Performance Impact of Using dict() Instead of {} in C... by ra on Wednesday November 14, 2012 @08:26AM (#4783176)
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.

Re:The Performance Impact of Using dict() Instead of {} in C... by Flow on Wednesday November 14, 2012 @11:06AM (#4783885)
Can't you just alter the settings for those code-warnings?
Re:The Performance Impact of Using dict() Instead of {} in C... by ra on Friday November 16, 2012 @11:27PM (#4797183)
Oh wow you'e totally right :-)

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