why generator assigned to slice?
Peter Otten
__peter__ at web.de
Wed Jan 5 09:12:07 EST 2011
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
More information about the Python-list
mailing list