why generator assigned to slice?

A

ana sanchez

hi!!!

i found this when i read the source of a program in python:

self.__chunks[start:end] = (chunk for i in xrange(start, end))

and also this:

self.__lines[line:line] = (None for i in xrange(count))

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

self.__chunks[line:line] = [None for i in xrange(count)]

thanks!!!

ana

p.d. excuse my english
 
P

Peter Otten

ana said:
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
 
A

ana sanchez

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top