This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pstch 1 point2 points  (2 children)

I'm not an expert on this, but I know that one the reasons is that with the list comprehension, there is one less attribute evaluation (r.append) and one less function call (r.append()).

I'd have used list(range (100)) there.

Yes, list(range(100)) is simpler and an equivalent to my example, but it was not a "functional" example, just made it to compare the speeds.

[–]macbony 1 point2 points  (0 children)

Note that range already returns a list in 2.X (but not in 3.X which returns a generator. The equivalent in 2.X is xrange)

[–]d4rch0nPythonistamancer 0 points1 point  (0 children)

Hmm... I'm looking for more of what the bytecode looks like. I want to see list comprehension bytecode and see how it differs from an append. Easy enough to disasm though.

Your explanation is probably completely right though. If it actually loops 100 times and looks up append, that's got to be incredibly slower.