Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Matter cannot be created or destroyed, nor can it be returned without a receipt.


devel / comp.lang.python / Fwd: Re: sum() vs. loop

SubjectAuthor
o Fwd: Re: sum() vs. loopAlan Gauld

1
Fwd: Re: sum() vs. loop

<mailman.961.1634081601.4164.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20030&group=comp.lang.python#20030

  copy link   Newsgroups: comp.lang.python
Path: rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: learn2program@gmail.com (Alan Gauld)
Newsgroups: comp.lang.python
Subject: Fwd: Re: sum() vs. loop
Date: Tue, 12 Oct 2021 23:55:35 +0100
Lines: 41
Message-ID: <mailman.961.1634081601.4164.python-list@python.org>
References: <b32e2da3-6617-5589-155a-bc9cc4fc5731@yahoo.co.uk>
Reply-To: alan.gauld@yahoo.co.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de kkc/bkCqcZowHhjEnB32eA2/c7lmhvSmC5IqD5JaXtzQ==
Return-Path: <learn2program@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=Bppvex9a;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02; 'def': 0.04;
'loop': 0.07; 'url-ip:65/8': 0.07; 'url-ip:162.219/16': 0.09;
'flickr': 0.16; 'from:name:alan gauld': 0.16; 'keller': 0.16;
'loops': 0.16; 'message-id:@yahoo.co.uk': 0.16; 'photo-blog':
0.16; 'purely': 0.16; 'reply-to:addr:alan.gauld': 0.16;
'seconds.': 0.16; 'subject:() ': 0.16; 'subject:loop': 0.16; 'url-
ip:65.9/16': 0.16; 'url-ip:79.170.44.132/32': 0.16; 'url-
ip:79.170.44/24': 0.16; 'url-ip:79.170/16': 0.16; 'url-ip:79/8':
0.16; 'url:alan-g': 0.16; 'url:alan_gauld': 0.16;
'url:alangauldphotos': 0.16; 'wrote:': 0.16; 'python': 0.16;
'url:amazon': 0.19; 'to:addr:python-list': 0.20; 'version': 0.23;
'subject:Fwd': 0.26; 'function': 0.27; 'header:User-Agent:1':
0.30; 'program': 0.31; 'received:192.168.1': 0.32; 'but': 0.32;
"i'm": 0.33; 'someone': 0.34; 'received:google.com': 0.34; 'runs':
0.35; 'from:addr:gmail.com': 0.35; 'received:209.85': 0.37;
'author': 0.37; 'received:192.168': 0.37; 'received:209': 0.39;
'single': 0.39; 'list': 0.39; 'steve': 0.39; 'alan': 0.40;
'explain': 0.40; 'seconds': 0.40; 'learn': 0.40; 'follow': 0.62;
'once': 0.63; 'expert': 0.64; 'reply-to:addr:yahoo.co.uk': 0.69;
'url:author': 0.69; 'site': 0.70; 'addition': 0.71; 'subject:. ':
0.73; 'reply-to:no real name:2**0': 0.78; 'header:Reply-To:1':
0.79; 'pairs': 0.84; 'subject:sum': 0.84; 'products': 0.88;
'million': 0.89
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=from:subject:reply-to:to:message-id:date:user-agent:mime-version
:content-transfer-encoding:content-language;
bh=/4RaVDjy5N+NFXHpivE61ZJfH46zLaBQW4ba27BSyJI=;
b=Bppvex9aXImxjavdMRYkJcOvzOoj4cGmEicd4q5XZ2EdXeuTkZ92+1NlUYvHalHrUb
3UCUTvb3e5brrCKXg5T+g8xL/dSkRqdXa5NIeuBjxvs/XEvJcDXDCw8OqSnbgAixV6gQ
fvN/49gEcIwZRuhZv0CFI0t5arZfx+HCsUiVt2daeF5H04WONezjwh2N/OmadhtUQYLr
pN8DsXF8T+V3lMSzXfPojSvRyAGtSp5w3KD5dFLAwKDokY3Wk2XtfqyCc0iOQkx40lBv
m/FnXDuYz3qgXaGvBkAyPZZ7unaky4Q8jfSR+C8lOOTyl7r3CFpVHKTHwbdZ4OXZsHhU
bsAg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:from:subject:reply-to:to:message-id:date
:user-agent:mime-version:content-transfer-encoding:content-language;
bh=/4RaVDjy5N+NFXHpivE61ZJfH46zLaBQW4ba27BSyJI=;
b=s86L6uj3F6nlKspE5YSv+/m7Rj9GUH7nA/zBGb16F5gXodA7R6VjAK1V4y95O0qjCk
TwymoKlxvvm+WXQ3F+/sotwt57EIbLawdfF0y+qMUP3hBSEreiKl7Eb9SApdGkcQdc9E
kJhhycfSRJDBRLBxsvwp7qdZcVssEH2rFpnLnGu4E1zVBdt+70EDxa/ybLVZIVzd69sy
f6W2DIVN6f1xRyMyS8cdX8i1MulT6Bc+ty4egUHsZVt9UHKSsvGVTf0RfBqju7ujtLNk
7hpPlJw4Ge99cCmQRGb3aJPY0z7qC1Plkyo94yqDT6ZJiwhP65myczpTntBouNBuVfjW
bjaQ==
X-Gm-Message-State: AOAM532wJm3FJjMkAo5oouu7ykwriYye+TmIopyDhdg4GXbbyAJV/hfK
xB1Xctomy7RKDMMaeeK49QzPy284sI1yzg==
X-Google-Smtp-Source: ABdhPJzu3KN6Ojt379/rhgPt2DBgqrnqoIWd1aR+US9qrwfJ2jZm90a16CloaICljmjti66H6hISAQ==
X-Received: by 2002:adf:ee8b:: with SMTP id b11mr18284692wro.349.1634079335949;
Tue, 12 Oct 2021 15:55:35 -0700 (PDT)
X-Google-Original-From: Alan Gauld <alan.gauld@yahoo.co.uk>
X-Forwarded-Message-Id:
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.13.0
Content-Language: en-GB
X-Mailman-Approved-At: Tue, 12 Oct 2021 19:33:21 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <b32e2da3-6617-5589-155a-bc9cc4fc5731@yahoo.co.uk>
 by: Alan Gauld - Tue, 12 Oct 2021 22:55 UTC

On 10/10/2021 09:49, Steve Keller wrote:
> I have found the sum() function to be much slower than to loop over the
> operands myself:
>
> def sum_products(seq1, seq2):
> return sum([a * b for a, b in zip(seq1, seq2)])
>
> def sum_products2(seq1, seq2):
> sum = 0
> for a, b in zip(seq1, seq2):
> sum += a * b
> return sum
>
> In a program I generate about 14 million pairs of sequences of ints each
> of length 15 which need to be summed. The first version with sum() needs
> 44 seconds while the second version runs in 37 seconds.
>
> Can someone explain this difference?

I'm no expert on Python innards but it looks to me like the first
version loops over the length of the list twice, once to generate
the list of products and the second time to add them together.

The pure Python version only loops once because it does the addition
in transit.

Presumably, especially for large numbers, a single python loop
is faster than 2 C loops?

But that's purely my speculation.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


devel / comp.lang.python / Fwd: Re: sum() vs. loop

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor