This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients alex, brett.cannon, rhettinger, terry.reedy
Date 2012-03-02.21:18:25
SpamBayes Score 2.6669777e-12
Marked as misclassified No
Message-id <1330723109.31.0.605324234903.issue14126@psf.upfronthosting.co.za>
In-reply-to
Content
I think this proposal should be rejected for three reasons.

1. I believe the idea is faulty in that it can only work if the single source iterable *has* a known length. The compiler cannot, in general, determine that and in practice seldom would be able to. For a comprehension within a function, the source typically is or depends on a passed arg. What happens if you replace 'range(10000)' with 'iter(range(10000))' in your patched version and rerun?

As I remember, Guido has rejected the idea of iterators having length information because in general it is not possible.

2. It adds an opcode for an extremely limited case. In 3.x, there are list, set, dict, and generator expression-comprehensions. Many 2.x uses of list comprehensions are (can be, increasingly will be) replaced by one of the others. In particular, lists long enough for there to be a real time saving tend to be replaced by generator expressions if possible.

3. The relative time saving in this limited case is at best 16% (.9/5.7) in a toy 2.7 example. I suspect it would be less in the more complex 3.x implementation and know it would be less in any realistic example with a real, slower target expression (at minimum try '2*j+1' or 'float(j)') and a slower source producer (such as a file object). And to repeat, a source with millions of items will likely be processed an item at a time without creating an explicit list.
History
Date User Action Args
2012-03-02 21:18:29terry.reedysetrecipients: + terry.reedy, brett.cannon, rhettinger, alex
2012-03-02 21:18:29terry.reedysetmessageid: <1330723109.31.0.605324234903.issue14126@psf.upfronthosting.co.za>
2012-03-02 21:18:25terry.reedylinkissue14126 messages
2012-03-02 21:18:25terry.reedycreate