Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail From: Bart Kastermans Newsgroups: comp.lang.python Subject: List comprehension timing difference. Date: Thu, 01 Sep 2011 18:35:10 -0600 Organization: MB-NET.NET for Open-News-Network e.V. Lines: 41 Message-ID: <87zkinkcht.fsf@gmail.com> NNTP-Posting-Host: kasterma.colorado.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: gwaiyur.mb-net.net 1314923713 6165 128.138.150.65 (2 Sep 2011 00:35:13 GMT) X-Complaints-To: abuse@open-news-network.org NNTP-Posting-Date: Fri, 2 Sep 2011 00:35:13 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) X-User-ID: U2FsdGVkX18X9mPSbmlQL+3aVvmop/lCJDsYTZsCah+tudSyriOMkBQPKzONpjiz Cancel-Lock: sha1:YZyFmihbFGGO4D2wqvgHgjI9PAw= sha1:A1uWpmwChS+9KaJGCzMEnnVm+mI= Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12596 In the following code I create the graph with vertices sgb-words.txt (the file of 5 letter words from the stanford graphbase), and an edge if two words differ by one letter. The two methods I wrote seem to me to likely perform the same computations, the list comprehension is faster though (281 seconds VS 305 seconds on my dell mini). Is the right interpretation of this timing difference that the comprehension is performed in the lower level C code? As this time I have no other conjecture about the cause. --------------------------------------------------------- import time import copy data = map (lambda x: x.strip(), open('sgb-words.txt').readlines()) def d (w1, w2): count = 0 for idx in range(0,5): if w1[idx] != w2[idx]: count += 1 return count print "creating graph" t0 = time.clock () graph = [[a,b] for a in data for b in data if d(a,b) ==1 and a < b] t1 = time.clock () print "took " + str (t1 - t0) + " seconds." t0 = time.clock () 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]) t1 = time.clock () print "took " + str (t1 - t0) + " seconds."