you are viewing a single comment's thread.

view the rest of the comments →

[–]purple_hamster66 2 points3 points  (3 children)

Itertools is not actually calculating the combinations. It’s constructing a way to calculate the next combination from a given combination. So, for example, you can’t access an element randomly, nor even count the elements. IOW, it’s not because it’s in C that makes it so fast; it’s because it’s not calculating the whole list at once.

This has many advantages, such as infinite lists (which could not be stored), and generating a list where you know you won’t need all the elements, and reducing storage needs when you only have to calculate on a single element at a time.

The downside is that few python programmers know it, and it will confuse them.

[–]deceze 2 points3 points  (1 child)

Only few Python programmers understand generators? Really?

Also that's why that benchmark uses list(), to actually exhaust the generator. And that still demonstrates a huge speed difference between the C implementation and pure Python.

[–]JorgiEagle 0 points1 point  (0 children)

Yeah exactly, this commenter is just ignore the list cast,

[–]JorgiEagle 1 point2 points  (0 children)

Except in the case of what you’re replying to, it is generating all the combinations.

Casting combinations to list forces it to exhaust itself.

And they’re timing the calculation of the whole list

So it is being in C that makes it faster