Re: Performance of lists vs. list comprehensions

2010-01-20 Thread Dave Angel
Steven D'Aprano wrote: A million items is not a lot of data. Depending on the size of each object, that might be as little as 4 MB of data: L = ['' for _ in xrange(10**6)] sys.getsizeof(L) 4348732 Note that this sys.getsizeof() is only saying the size of the list, not the size

Re: Performance of lists vs. list comprehensions

2010-01-20 Thread Alf P. Steinbach
* Steven D'Aprano: On Wed, 20 Jan 2010 05:25:22 +0100, Alf P. Steinbach wrote: * Steven D'Aprano: On Tue, 19 Jan 2010 16:20:42 -0500, Gerald Britton wrote: That's surprising. I wouldn't implement it that way at all. I'd use a dynamically-expanding buffer as I suggested. That way you get a

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Stefan Behnel
Steven D'Aprano, 20.01.2010 07:12: > On Wed, 20 Jan 2010 05:25:22 +0100, Alf P. Steinbach wrote: >> That is a good argument for not doing the expanding buffer thing. >> But such buffers may be generally present anyway, resulting from >> optimization of "+". > > As near as I can determine, the CPyt

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Steven D'Aprano
On Wed, 20 Jan 2010 05:25:22 +0100, Alf P. Steinbach wrote: > * Steven D'Aprano: >> On Tue, 19 Jan 2010 16:20:42 -0500, Gerald Britton wrote: >> >>> That's surprising. I wouldn't implement it that way at all. I'd use a >>> dynamically-expanding buffer as I suggested. That way you get a >>> sing

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Alf P. Steinbach
* Steven D'Aprano: On Tue, 19 Jan 2010 16:20:42 -0500, Gerald Britton wrote: That's surprising. I wouldn't implement it that way at all. I'd use a dynamically-expanding buffer as I suggested. That way you get a single pass and don't have to calculate anything before you begin. In the best ca

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Steven D'Aprano
On Tue, 19 Jan 2010 16:20:42 -0500, Gerald Britton wrote: > That's surprising. I wouldn't implement it that way at all. I'd use a > dynamically-expanding buffer as I suggested. That way you get a single > pass and don't have to calculate anything before you begin. In the best > case, you'd use

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Steven D'Aprano
On Tue, 19 Jan 2010 11:26:43 -0500, Gerald Britton wrote: > Interestingly, I scaled it up to a million list items with more or less > the same results. A million items is not a lot of data. Depending on the size of each object, that might be as little as 4 MB of data: >>> L = ['' for _ in xrang

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Arnaud Delobelle
Gerald Britton writes: > That's surprising. I wouldn't implement it that way at all. I'd use a > dynamically-expanding buffer as I suggested. That way you get a > single pass and don't have to calculate anything before you begin. In > the best case, you'd use half the memory (the result just f

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Raymond Hettinger
[Wolfram Hinderer] > Yes, list building from a generator expression *is* expensive. And > join has to do it, because it has to iterate twice over the iterable > passed in: once for calculating the memory needed for the joined > string, and once more to actually do the join (this is implementation >

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
That's surprising. I wouldn't implement it that way at all. I'd use a dynamically-expanding buffer as I suggested. That way you get a single pass and don't have to calculate anything before you begin. In the best case, you'd use half the memory (the result just fits in the buffer after its last

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Wolfram Hinderer
On 19 Jan., 21:06, Gerald Britton wrote: > [snip] > > > > > Yes, list building from a generator expression *is* expensive. And > > join has to do it, because it has to iterate twice over the iterable > > passed in: once for calculating the memory needed for the joined > > string, and once more to

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Arnaud Delobelle
Gerald Britton writes: > [snip] > >> >> Yes, list building from a generator expression *is* expensive. And >> join has to do it, because it has to iterate twice over the iterable >> passed in: once for calculating the memory needed for the joined >> string, and once more to actually do the join (

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
[snip] > > Yes, list building from a generator expression *is* expensive. And > join has to do it, because it has to iterate twice over the iterable > passed in: once for calculating the memory needed for the joined > string, and once more to actually do the join (this is implementation > dependen

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Wolfram Hinderer
On 19 Jan., 16:30, Gerald Britton wrote: > >>> Timer("' '.join([x for x in l])", 'l = map(str,range(10))').timeit() > > 2.9967339038848877 > > >>> Timer("' '.join(x for x in l)", 'l = map(str,range(10))').timeit() > > 7.2045478820800781 [...] > 2. Why should the "pure" list comprehension be slow

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Interestingly, I scaled it up to a million list items with more or less the same results. It's helpful to see that your results are different. That leads me to suspect that mine are somehow related to my own environment. Still I think the key is the overhead in calling next() for each item in th

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Stephen Hansen
On Tue, Jan 19, 2010 at 7:30 AM, Gerald Britton wrote: [snip] > mystring = '\n'.join( line for line in lines if depending on line> ) > Note, this is not a list comprehension, but a generator comprehension. A list comprehension is used to build, in one sweep, a list and return it. A generator c

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Thanks! Good explanation. On Tue, Jan 19, 2010 at 10:57 AM, Alf P. Steinbach wrote: > * Gerald Britton: >> >> Yesterday I stumbled across some old code in a project I was working >> on.  It does something like this: >> >> mystring = '\n'.join( [ line for line in lines if > depending on line> ] )

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Alf P. Steinbach
* Gerald Britton: Yesterday I stumbled across some old code in a project I was working on. It does something like this: mystring = '\n'.join( [ line for line in lines if ] ) where "lines" is a simple list of strings. I realized that the code had been written before you could put a list compr

Performance of lists vs. list comprehensions

2010-01-19 Thread Gerald Britton
Yesterday I stumbled across some old code in a project I was working on. It does something like this: mystring = '\n'.join( [ line for line in lines if ] ) where "lines" is a simple list of strings. I realized that the code had been written before you could put a list comprehension as an argum