Possible memory leak?
Giovanni Bajo
noway at sorry.com
Wed Jan 25 18:20:08 EST 2006
Fredrik Lundh wrote:
> since you know the length, you can preallocate the list
>
> def iters3(n):
> L = [None]*n
> for i in xrange(n):
> L[i] = chr(i%64)
> return "".join(L)
>
> or use a preallocated array
>
> def iters4(n):
> L = array.array("B", [0])*n
> for i in xrange(n):
> L[i] = i%64
> return L.tostring()
Of course. I was just trying to make a point about string accumulation being
O(n) and not O(n^2). You can get better factors with other solutions like the
ones you propose, but even the "simple" implementation (and most readable and
Pythonic, IMHO) behaves "well" under CPython 2.4.
--
Giovanni Bajo
More information about the Python-list
mailing list