On 12/23/19 8:09 PM, Kyle Stanley wrote:

Chris Angelico wrote:
> I think this is an artifact of Python not having an empty set literal.
> [snip]
> When both use the constructor call or both use a literal, the
> difference is far smaller. I'd call this one a wash.

Ah, good catch. I hadn't considered that it would make a substantial difference, but that makes sense. Here's an additional comparison between "{}"  and "dict()" to confirm it:

>>> timeit.timeit("{}", number=100_000_000)
2.1038335599987477
>>> timeit.timeit("dict()", number=100_000_000)
10.225583500003268


We could also go the other way with it, set / dict-map-to-dontcare with one element, because that way we can have a set literal:

>>> timeit.timeit("set()", number=100_000_000)
8.579900023061782
>>> timeit.timeit("dict()", number=100_000_000)
10.473437276901677
>>> timeit.timeit("{0}", number=100_000_000)
5.969995185965672
>>> timeit.timeit("{0:0}", number=100_000_000)
6.24465325800702

(ran all four of those just so you see a relative sense on my laptop, which I guess is only sliiiightly slower than Kyle's)


Happy holidays,


/arry