If there’s one thing that really grinds my gears, it’s when people use lists instead of tuples to define what is intended to be an immutable constant. Why? Because it’s faster to create a tuple. Here’s a case as an example:
Clearly, as a class attribute, the initial developer has intended that SomeClass.AVAILABLE_KEYS is some sort of constant. If it were me seeing this, it would bother me that we could easily make the creation of this list microseconds faster with a minor change. Just to see how correct I am, I set out to do a basic experiment to capture just how long it takes to create common datastrucutres in python. So I hacked together a simple script:
…and now we can find out just how long it takes to compare the creation time of lists, tuples, sets, and dictionaries. I threw in sets and dictionaries just because I had already started the experiment, but to begin with my initial qualm, here’s the comparison of time between lists and tuples given a number of elements:
How Right Am I?
Clearly, there’s an extra 6 to 8 microseconds of overhead associated with lists. This means that in a fairly optimal request/response to and from a web server of 200 ms, we’re increasing the response time by about 0.0034% totally unnecessarily! So, I’m totally right. Sort of.
Time for Tuples / List / Sets / Dictionaries
The results for other common python datastructures are pictured above. Obviously, creation time is O(n) for every type, but I was actually fairly surprised to see how much longer a set or a dictionary took to create relative to a list/tuple. For me, this changes my perspective a bit for statically defined constants. Even though we’re only talking about microseconds here, it’s still a matter of principal for me. The question for me is essentially what should be favored between smaller time complexity and datastructure overhead if we have a fixed number of elements.
For example, consider the following 2 list comprehensions:
Which is faster? Does it really even matter? In the second case, the number of iterations across the entire list comprehension will be much smaller by some kind of quadratic factor, but does that make anything faster when casting a tuple to a set incurs significant (microseconds) overhead?
Clearly I’m splitting hairs here, but all in all, this is at the very least insightful knowledge that could easily be shared at a cocktail party, and in sharing it you would get an insurmountable amount of high fives.