Re: shouldn't list comprehension be faster than for loops?

2009-12-19 Thread Steven D'Aprano
On Sat, 19 Dec 2009 12:28:32 +1100, Ryan Kelly wrote: Anything else being equal, list comprehensions will be the faster becuase they incur fewer name and attribute lookups. It will be the same as the difference between a for loop and a call to map. A list comprehension is basically an

Re: shouldn't list comprehension be faster than for loops?

2009-12-19 Thread sturlamolden
On 19 Des, 02:28, Ryan Kelly r...@rfk.id.au wrote: Not so.  If you use the dis module to peek at the bytecode generated for a list comprehension, you'll see it's very similar to that generated for an explicit for-loop.  The byte-code for a call to map is very different. First, you failed to

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread Carlos Grohmann
Have you tried this with    dip1 = [dp - 0.01 if dp == 90 else dp for dp in dipList] Yes that is better! many thanks! -- http://mail.python.org/mailman/listinfo/python-list

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread sturlamolden
On 17 Des, 18:37, Carlos Grohmann carlos.grohm...@gmail.com wrote: Tenting the time spent by each approach (using time.clock()), with a file with about 100,000 entries, I get 0.03s for the loop and 0.05s for the listcomp. thoughts? Anything else being equal, list comprehensions will be the

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread sturlamolden
On 17 Des, 18:42, Alf P. Steinbach al...@start.no wrote: Have you tried this with    dip1 = [dp - 0.01 if dp == 90 else dp for dp in dipList] And for comparison with map: map(lambda dp: dp - 0.01 if dp == 90 else dp, dipList) -- http://mail.python.org/mailman/listinfo/python-list

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread Carl Banks
On Dec 17, 9:37 am, Carlos Grohmann carlos.grohm...@gmail.com wrote: Tenting the time spent by each approach (using time.clock()), with a file with about 100,000 entries, I get 0.03s for the loop and 0.05s for the listcomp. thoughts? You shouldn't trust your intuition in things like this.

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread sturlamolden
On 17 Des, 18:37, Carlos Grohmann carlos.grohm...@gmail.com wrote: Tenting the time spent by each approach (using time.clock()), with a file with about 100,000 entries, I get 0.03s for the loop and 0.05s for the listcomp. thoughts? Let me ask a retoric question: - How much do you really

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread Brian J Mingus
On Fri, Dec 18, 2009 at 11:55 AM, sturlamolden sturlamol...@yahoo.nowrote: On 17 Des, 18:37, Carlos Grohmann carlos.grohm...@gmail.com wrote: Tenting the time spent by each approach (using time.clock()), with a file with about 100,000 entries, I get 0.03s for the loop and 0.05s for the

Re: shouldn't list comprehension be faster than for loops?

2009-12-18 Thread Ryan Kelly
Tenting the time spent by each approach (using time.clock()), with a file with about 100,000 entries, I get 0.03s for the loop and 0.05s for the listcomp. Anything else being equal, list comprehensions will be the faster becuase they incur fewer name and attribute lookups. It will be the

shouldn't list comprehension be faster than for loops?

2009-12-17 Thread Carlos Grohmann
Hello all I am testing my code with list comprehensions against for loops. the loop: dipList=[float(val[1]) for val in datalist] dip1=[] for dp in dipList: if dp == 90: dip1.append(dp - 0.01) else: dip1.append(dp) listcomp:

Re: shouldn't list comprehension be faster than for loops?

2009-12-17 Thread Alf P. Steinbach
* Carlos Grohmann: Hello all I am testing my code with list comprehensions against for loops. the loop: dipList=[float(val[1]) for val in datalist] dip1=[] for dp in dipList: if dp == 90: dip1.append(dp - 0.01) else: dip1.append(dp)