Debugging reason for python running unreasonably slow when adding numbers

Peter J. Holzer hjp-python at hjp.at
Sat Mar 18 08:20:42 EDT 2023


On 2023-03-15 17:09:52 +0000, Weatherby,Gerard wrote:
> Sum is faster than iteration in the general case.

I'd say this is the special case, not the general case.

> def sum1():
>     s = 0
>     for i in range(1000000):
>         s += i
>     return s
> 
> def sum2():
>     return sum(range(1000000))

Here you already have the numbers you want to add.

The OP needed to compute those numbers first.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20230318/f5e1b095/attachment.sig>


More information about the Python-list mailing list