Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing joining list with a 60MB string with `timeit` causes MemoryError

Tags:

python

timeit

My test appends creates a list containing a 60MB string and a 5-byte string. This list is then joined with join():

import timeit
setup_str = 'str_5byte = "\xfa\xea\x02\x02\x02"; L = [str_5byte]; str_60mb = str_5byte * 12000000'
t = timeit.Timer('L.append(str_60mb); str_long = "".join(L)', setup=setup_str)
t.timeit(100)

Returns this exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python25\lib\timeit.py", line 161, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 6, in inner
MemoryError

I assume that after every execution the variables are deleted and garbage-collected, so why am I running out of memory? Running the test with 8 executions is OK, but higher than that and I get this error.

like image 273
DBedrenko Avatar asked Mar 31 '26 18:03

DBedrenko


1 Answers

Yes, with t.timeit the setup statement is executed only once and then the test statement multiple times. This means the list L persists and grows every iteration, resulting obviously in your system running out of memory.

Try min(t.repeat(repeat=100, number=1)) to execute the setup each time before the evaluation of the test statement.

Here's the docs if you need more info.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!