Re:Loops in Python – Comparison and Performance
by chronial on Thursday August 08, 2019 @01:11PM
(#20646351)
Note that this article is missing the most pythonic implementation, which is
This is also twice as fast as their fastest pure-python implementation on my machine.
[ Parent
| on HN ]
Re:Loops in Python – Comparison and Performance
by BerislavLopac on Friday August 09, 2019 @06:05AM
(#20652863)
Or even better, and likely faster for huge lists:
z = list(map(sum, zip(x, y)))
Even that list is superfluous; the map function returns a generator, so it's better you just need one number at a time but possibly not the whole list (e.g. loop through results until you find a certain value).
Re:Loops in Python – Comparison and Performance
by dagw on Friday August 09, 2019 @08:54AM
(#20653817)
likely faster for huge lists:
Nope, at least not on my computer. The map solution was ~50% slower than the list comprehensions when run a list with 1 million ints, and almost twice as slow when run on a list with 100k ints.
In fact even using a for loop in the obvious way was slightly faster than map with 1 million elements (about halfway between the map and list comprehension performance wise).
Map without the last list call is however very slightly faster than the equivalent generator comprehension.
Re:Loops in Python – Comparison and Performance
by BerislavLopac on Friday August 09, 2019 @06:23PM
(#20658679)
That is interesting. It's possible that they have done some optimisations on loops since the last time I measured it, but that was back in Python 2.6 (I think), and map was much faster than a loop (no comprehensions back then), and it was particularly obvious on very high number of input elements.
Also, from your description I suspect that the list() call over a generator might introduce extra overhead, rather than map itself...
Re:Loops in Python – Comparison and Performance
by dagw on Saturday August 10, 2019 @04:04AM
(#20660818)
Your last comment seems reasonable. I guess the new rule of thumb should be maps are still fast, but only if you want a generator. If you need a list use a list comprehension.
Re:Loops in Python – Comparison and Performance
by metalliqaz on Thursday August 08, 2019 @02:16PM
(#20647063)
Thank you I was just thinking that the author ruined the credibility of the tests by not using pythonic code.
Re:Loops in Python – Comparison and Performance
by voldacar on Thursday August 08, 2019 @07:50PM
(#20650109)
For performance purposes, is there something especially important about zipping the two arrays into one array of tuples before adding them?
Re:Loops in Python – Comparison and Performance
by BerislavLopac on Friday August 09, 2019 @06:24AM
(#20652944)
Zip does not create an array, it creates a generator which returns a tuple on each pass.