0

I am learning python and was checking out the timeit module. I am trying the exact same thing with the list, set and tuple to check the overheads involved. Please take a gander (IPython shell) :-

>>> timeit {x**9 for x in range(100)}
>>> 49.3 µs ± 229 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

>>> timeit (x**9 for x in range(100))
>>> 830 ns ± 6.39 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

>>> timeit [x**9 for x in range(100)]
>>> 45.8 µs ± 346 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

I get that tuple comprehension is fastest because it doesn't have any of the overhead of set and list, eg. hash calculation and duplicate removal in set or order preservation of elements in list.

But what I do not understand is that the set comprehension and list comprehension take almost equivalent time. I want to know why is the set comprehension is not very much slower than the list comprehension as in a set, the compiler has the overhead of computing the hash value of every entry and check for duplicates while list comprehension don't have to.

Thanks in advance for your time.

Gsbansal10
  • 303
  • 5
  • 12
  • 6
    `(x**9 for x in range(100))` is *not* a tuple comprehension. It is a generator, which has to be looped over again to access each value that is yielded at a particular iteration. Thus, that is why the latter is supposedly the fastest, because only the generator object has been created. – Ajax1234 Nov 09 '18 at 05:02
  • 2
    `%timeit tuple(x**9 for x in range(100))` -> 48.5 µs (51.8 µs for the set and 43.3 µs for the list) – cs95 Nov 09 '18 at 05:06
  • 2
    Also, *hashing is the duplicate removal*. There isn't two separate things going on. It is the nature of a hash set that it will only create an array with values unique by hash. The uniqueness comes for free. But as others have pointed out, it should be **very curious** that the "tuple comprehension" is that much faster. `tuple` and `list` object constructions have almost exactly the same overhead, except lists are often a tiny bit less memory efficient. But what you have here is a *generator expression*, not a "tuple comprehension", which doesn't exist. – juanpa.arrivillaga Nov 09 '18 at 05:51
  • @juanpa.arrivillaga thanks, you were lucid enough to clear the cobwebs of doubts. :-) – Gsbansal10 Nov 09 '18 at 08:01

0 Answers0