3 ms·
It's not brilliant. This accomplishes the same thing without being hard to understand: from itertools import islice iterator = iter(array) try:
by signed0 13y ago
It's not brilliant.
This accomplishes the same thing without being hard to understand:
from itertools import islice
iterator = iter(array)
try:
while True:
yield list(islice(iterator, 3))
except StopIteration:
pass
- StavrosK 13y agoSure, there are many ways to do it, but I think the author was going for speed here.
- signed0 13y agoI'd be surprised if my way is slower. Any time you unpack into a function such as zip() python has to create an intermediary list to store all the results before calling the function.
- merlincorey 13y agoExactly, I would be very surprised if the zip version was faster. One of the first steps to optimization in python is moving everything you can to generators and using of itertools. The OP's question of is this genius or bad is clear in that regard: it is bad, due to not being the proper optimization direction, but it is interesting.
- StavrosK 13y agoI would be very surprised if it weren't. The zip version has the hot path written in C, the other version has it written in Python with a bunch of exception handling/list assignment code, etc. The original method is just zip(iterator, iterator, iterator), which is probably as fast as anything can be.
- elyase 13y agoThat is a common misconception. Moving to iterators adds a function call while list creation in C is quite fast. Every case has to be tested for performance.
- mctx 13y agoI always thought the reason iterators are used in preference to lists was due to the memory advantages, not the performance.
- ekimekim 13y agoThere's also flexibility improvements to do with lazy evaluation, and it makes the Twisted guys not hate you.
- merlincorey 13y agoThe memory advantages are key to performance when you have a non-trivially sized dataset, in my experience.
- merlincorey 13y agoIn my experience, building a non-trivially transformed sequence with the builtin C-backed list and list comprehensions vs iterators and generators, the iterators and generators win. Maybe I'm doing crazy stuff, though!
- kroger 13y agoYou could always use izip: http://docs.python.org/2/library/itertools.html#itertools.izip http://docs.python.org/2/library/itertools.html#itertools.iz...
- deleted 13y ago[deleted]
- signed0 13y agoI was a bit wrong about that. For some reason I imagined that there would be quite a few args being passed to zip, when in fact there are just the three iterators. It does create a temporary list, but it's so small it's negligible. Using izip wouldn't really change anything.
- merlincorey 13y agoDid you test and time the itertools version?
- StavrosK 13y agoI did not, but I might now. EDIT: See update above.
- StavrosK 13y agoThis crashed my computer (it's an infinite loop that needs too much memory, apparently), and there's a mistake (the i is not defined). Turns out that islice doesn't raise an IterationError, it just returns an empty list. Fixing the problems, it runs in 237 μsec per loop, around 23 times more than the zip version.
- signed0 13y agoHaha. I feel very silly now. I was just about to respond with the same thing. while True: result = list(islice(iterator, 3)) if not result: break yield result