I've recently looked into using list()
, dict()
, tuple()
in place of []
, {}
, and ()
, respectively when needing to create an empty one of of the three. The reasoning is that it seemed more readable. I was going to ask for opinions on the style, but then I decided to test performance. I did this:
>>> from timeit import Timer
>>> Timer('for x in range(5): y = []').timeit()
0.59327821802969538
>>> from timeit import Timer
>>> Timer('for x in range(5): y = list()').timeit()
1.2198944904251618
I tried dict()
, tuple()
and list()
and the function call version of each was incredibly worse than the syntactical version ({}
[]
, ()
) So, I have 3 questions:
- Why are the function calls more expensive?
- Why is there so much difference?
- Why the heck does it take 1.2 seconds to create 5 empty lists in my timer? I know
timeit
turns off garbage collection, but that couldn't possibly have an effect when considering I only usedrange(5)
.