Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12642
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail |
|---|---|
| From | Bart Kastermans <bkasterm@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: List comprehension timing difference. |
| Date | Fri, 02 Sep 2011 07:54:50 -0600 |
| Organization | MB-NET.NET for Open-News-Network e.V. |
| Lines | 52 |
| Message-ID | <87sjofjbh1.fsf@gmail.com> (permalink) |
| References | <87zkinkcht.fsf@gmail.com> <mailman.676.1314925972.27778.python-list@python.org> |
| NNTP-Posting-Host | kasterma.colorado.edu |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| X-Trace | gwaiyur.mb-net.net 1314971694 1602 128.138.150.65 (2 Sep 2011 13:54:54 GMT) |
| X-Complaints-To | abuse@open-news-network.org |
| NNTP-Posting-Date | Fri, 2 Sep 2011 13:54:54 +0000 (UTC) |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| X-User-ID | U2FsdGVkX18Ijpl7SIJi1OtL+pWPfjHSqNCv28gSLxEjBP9+kotzmzI66+3edWAz |
| Cancel-Lock | sha1:drLmV4wfl0qJu6xNIR8AVQA2Ceo= sha1:hyY9Q3MxpcNQH4ktSHyD+IOKzUU= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:12642 |
Show key headers only | View raw
MRAB <python@mrabarnett.plus.com> writes:
> On 02/09/2011 01:35, Bart Kastermans wrote:
>> graph = [[a,b] for a in data for b in data if d(a,b) ==1 and a< b]
>> graph2 = []
>> for i in range (0, len(data)):
>> for j in range(0,len(data)):
>> if d(data[i],data[j]) == 1 and i< j:
>> graph2.append ([i,j])
>
> Are they actually equivalent? Does graph == graph2?
>
> The first version (list comprehension) creates a list of pairs of
> values:
>
> [a, b]
>
> whereas the second version (for loops) creates a list of pairs of
> indexes:
>
> [i, j]
>
> The second version has subscripting ("data[i]" and "data[j]"), which
> will slow it down.
You are absolutely right. I had changed the code from the
equivalent:
graph2 = []
for i in range (0, len(data)):
for j in range(0,len(data)):
if d(data[i],data[j]) == 1 and i < j:
graph2.append ([data[i],data[j]])
But then also tried the equivalent
for a in data:
for b in data:
if d(a,b) == 1 and a < b:
graph2.append([a,b])
Which does away with the indexing, and is just about exactly as
fast as the list comprehension.
That'll teach me; I almost didn't ask the question thinking it might
be silly. And it was, but I thought it for the wrong reason. I tell my
students there are no stupid questions, I should listen to myself
more when I do. Thanks!
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
List comprehension timing difference. Bart Kastermans <bkasterm@gmail.com> - 2011-09-01 18:35 -0600
Re: List comprehension timing difference. MRAB <python@mrabarnett.plus.com> - 2011-09-02 02:12 +0100
Re: List comprehension timing difference. Bart Kastermans <bkasterm@gmail.com> - 2011-09-02 07:54 -0600
Re: List comprehension timing difference. ting@thsu.org - 2011-09-02 07:50 -0700
Re: List comprehension timing difference. Bart Kastermans <bkasterm@gmail.com> - 2011-09-02 20:15 -0600
csiph-web