On Tue, Oct 12, 2021 at 9:02 AM Stefan Ram <r...@zedat.fu-berlin.de> wrote: > > Steve Keller <keller.st...@gmx.de> writes: > >Now completely surprised. > > I have observed that here the generator-based sum() call > is slower if both seq1 and seq2 have a length of 1000, but > faster if both seq1 and seq2 have 10000000 entries each > (with float entries). > > However, in any case, the direct for-loop still is fastest. > Maybe, calling "next()" (actually, "PyIter_Next" in C) on > an iterator has some overhead compared to just evaluating > an expression in a loop. >
There's overhead whichever way you do it, and ultimately, it depends on what you're doing. The genexp is effectively a callback into Python code. The list comp requires preconstruction of the entire list. The plain for loop involves summing by picking up and putting down in Python code rather than doing that in C. The only way to know which is fastest is to try them all :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list