why generator assigned to slice?
ana sanchez
private at private.com
Thu Jan 6 08:44:08 EST 2011
In <ig1u77$4iv$00$1 at news.t-online.com> Peter Otten <__peter__ at web.de> writes:
>ana sanchez wrote:
>> i found this when i read the source of a program in python:
>>
>> self.__chunks[start:end] = (chunk for i in xrange(start, end))
>> what utility has to assign a generator to a slice??? ?the *final
>> result* isn't the same as this?:
>>
>> self.__chunks[start:end] = [chunk for i in xrange(start, end)]
>Whoever used the first variant probably was hoping that it was either faster
>or used less peak memory. I think the latter is wrong, and the former can
>easily be checked:
>$ python -m timeit -s'chunk = "yadda"; chunks = range(100); start = 20; end
>= 50' 'chunks[start:end] = (chunk for i in xrange(start, end))'
>100000 loops, best of 3: 9.02 usec per loop
>$ python -m timeit -s'chunk = "yadda"; chunks = range(100); start = 20; end
>= 50' 'chunks[start:end] = [chunk for i in xrange(start, end)]'
>100000 loops, best of 3: 4.16 usec per loop
>$ python -m timeit -s'chunk = "yadda"; chunks = range(100); start = 20; end
>= 50' 'chunks[start:end] = [chunk]*(end-start)'
>1000000 loops, best of 3: 1.02 usec per loop
peter thank you very much!!! (i like very much the timing "miniscripts")
ana
More information about the Python-list
mailing list