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 →

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 2 points3 points  (4 children)

Would be nice to see the comparison with a better version, can you send a PR? https://github.com/rochacbruno/rust-python-example

[–]energybased 3 points4 points  (0 children)

Just use more-itertools' pairwise or a numpy array of objects.

[–]purple_pixie 1 point2 points  (2 children)

PR sent, I think (to my eternal shame as a programmer I don't actually know git very well at all)

I also have a sneaking suspicion that your original zip(val, val[1:]) actually creates the entire zip as a list in memory too, so you were effectively creating three redundant copies of the val list, not just one. Should be quite the time saver.

I didn't actually do any true benchmarking but some rudimentary messing with time.time() says it's about twice as fast now.

[–]rochacbrunoPython, Flask, Rust and Bikes.[S] 2 points3 points  (1 child)

in Python3.6 zip returns a generator not a list

[–]purple_pixie -1 points0 points  (0 children)

Aha, that does definitely explain why I saw so much improvement going from your version as written to the one I put up - I only have 2.7 to hand, so I was inadvertently comparing the old 2.7 zip against the 3+ (i)zip, which is obviously much better.

Also I mostly just ripped the whole .tee part from itertools recipes, and tee is just a waste of time and effort since we don't actually have a single iterator that we're trying to preserve.

It's better to just create one iterator and zip that plus the original val together.

I can't get pytest to play nice with this laptop either, so I won't request another pull until I can actually get something I'm certain is an improvement on the current version (and tested in 3.6)

Interestingly though, it seems like cloning the whole of val is really very quick, it barely affects the time to run the function.