Cryptographically random numbers
Paul Rubin
http
Tue Mar 7 18:10:09 EST 2006
"Tuvas" <tuvas21 at gmail.com> writes:
> I've actually done the tests on this one, it's actually faster to use
> the += than a list, odd as it may sound.
Frederik explained the reason; there's an optimization in Python 2.4
that I'd forgotten about, for that specific case. It's not in earlier
versions. It's a bit fragile in 2.4:
a = ''
for x in something:
a += g(x)
is fast, but if a is aliased, Python can't do the optimization, so
a = ''
b = a
for x in something:
a += g(x)
is slow. Figuring out which case to use relies on CPython's reference
counting storage allocator (the interpreter keeps track of how many
pointers there are to any given object) and so the optimization may
not be feasible at all in other implementations which use different
storage allocation strategies (e.g. Lisp-style garbage collection).
All in all I think it's best to use a completely different approach
(something like StringBuffer) but my effort to start a movement here
on clpy a couple months ago didn't get anywhere.
More information about the Python-list
mailing list