generator expressions: performance anomaly?

Diez B. Roggisch deetsNOSPAM at web.de
Tue Jan 18 08:30:13 EST 2005


> Would it be a bad solution to make that a list or tuple of ten
> time.time() calls, you could also restrict what theese prebuilding
> sequenses could contain, or perhaps try to prebuild them, and then fail
> if it's impossible.

I don't fully understand what you mean. Restricting them would mean to add
static declarations that say "I return the same whenever you call me" -
which is even more than the already fragile "const"  declaration of c++
allows. 

And for your second suggestion - how exactly do you check for failing?
Generating 2 elements and checking if they are equal to the first two of
the old list? What about overloaded __cmp__/__eq__, or just the last
element differing?

This problem is too complex to solve automatically - domain knowledge is
needed. As the code Antoon presented also suggests the simple solution:


lst = list(time.time() for i in xrange(10))
tpl = tuple(lst)


I don't see why this is a desirable feature at all. Keep in mind that
putting statements or even stackframes between those two statements above
utterly complicates things. And we even didn't dig into the dynamic
features of python that for example make it possible to alter time.time
like this

time.time = random.random()

so that it behaves totally different - while the syntactically equivalence
still holds. No chance of catching that. 
-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list