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

2009-12-19 Thread sturlamolden
On 19 Des, 02:28, Ryan Kelly 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 realize t

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-18 Thread Gregory Ewing
Ryan Kelly wrote: Someone else wrote: It will be the same as the difference between a for loop and a call to map. 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 usua

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 wil

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 wrote: > On 17 Des, 18:37, Carlos Grohmann 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 as

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

2009-12-18 Thread sturlamolden
On 17 Des, 18:37, Carlos Grohmann 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 value 20 ms of CPU time

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

2009-12-18 Thread Carl Banks
On Dec 17, 9:37 am, Carlos Grohmann 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. Some features were add

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

2009-12-18 Thread sturlamolden
On 17 Des, 18:42, "Alf P. Steinbach" 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 sturlamolden
On 17 Des, 18:37, Carlos Grohmann 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 faster becuase they i

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-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) listcomp

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: dipList=[float