Using the literal syntax can give minor performance bumps compared to using function calls to create dict
, list
and tuple
.
In [1]: timeit.timeit(stmt="dict()", number=100000000)
Out[1]: 9.560388602000103
In [2]: timeit.timeit(stmt="{}", number=100000000)
Out[2]: 1.685333584000091
In [3]: timeit.timeit(stmt="tuple()", number=100000000)
Out[3]: 4.509182139000131
In [4]: timeit.timeit(stmt="()", number=100000000)
Out[4]: 0.5455615430000762
In [5]: timeit.timeit(stmt="list()", number=100000000)
Out[5]: 7.356000728000254
In [6]: timeit.timeit(stmt="[]", number=100000000)
Out[6]: 1.573771306999788
This is because here, the name dict
must be looked up in the global scope in case it has been rebound.
Same goes for the other two types list()
and tuple()
.