[Python-Dev] The `for y in [x]` idiom in comprehensions
Stefan Behnel
stefan_ml at behnel.de
Fri Feb 23 15:50:39 EST 2018
More information about the Python-Dev mailing list
Fri Feb 23 15:50:39 EST 2018
- Previous message (by thread): [Python-Dev] The `for y in [x]` idiom in comprehensions
- Next message (by thread): [Python-Dev] The `for y in [x]` idiom in comprehensions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Chris Barker schrieb am 23.02.2018 um 20:23: > BTW, would it be even a tiny bit more efficient to use a tuple in the inner > loop? > > [g(y) for x in range(5) for y in (f(x),)] Serhiy's optimisation does not use a loop at all anymore and folds it into a direct assignment "y=f(x)" instead. But in general, yes, changing a list iterable into a tuple is an improvement as tuples are more efficient to allocate. Haven't tried it in CPython (*), but it might make a slight difference for very short iterables, which are probably common. Although the execution of the loop body will likely dominate the initial allocation by far. Stefan (*) I implemented this list->tuple transformation in Cython a while ago, but seeing Serhiy's change now got me thinking that this could be further improved into a stack allocated C array, to let the C compiler unroll the loop at will. I'll probably try that at some point... https://github.com/cython/cython/issues/2117
- Previous message (by thread): [Python-Dev] The `for y in [x]` idiom in comprehensions
- Next message (by thread): [Python-Dev] The `for y in [x]` idiom in comprehensions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list