Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does timeit gain by turning off garbage collection?

I was reading the code for the timeit module and I noticed this segment:

gcold = gc.isenabled()
gc.disable()
timing = self.inner(it, self.timer)
if gcold:
    gc.enable()

This just stores the state of garbage collection (on or off) and then turns it off. The function inner executes the statement being timed. It then reverts the garbage collector to its old state.

So I'm curious about what the point of this is. If the code being tested works the garbage collector, then shouldn't that be reflected in the testing? What am I missing?

like image 710
aaronasterling Avatar asked Dec 24 '10 02:12

aaronasterling


People also ask

What does%% timeit do?

You can use the magic command %%timeit to measure the execution time of the cell. As an example, try executing the same process using NumPy . As with %timeit , -n and -r are optional. Note that %%timeit measures the execution time of the entire cell, so the following example includes the time to import NumPy.

What is Gc disable in Python?

Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles. Automatic collection can be disabled by calling gc. disable() . To debug a leaking program call gc.

How do you collect garbage in Python?

Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes.


2 Answers

The nature of garbage collection is that its frequency and duration is somewhat unpredictable. At least from the point of view of the performance of a specific chunk of code, the garbage collector just adds noise to the statistics.

From a whole-program point of view, you are correct in that if your program is running on a system where the garbage collector is exercised, then yes that should be taken into account when measuring the program performance. But it's not usually factored in for individual functions.

like image 132
Greg Hewgill Avatar answered Oct 28 '22 19:10

Greg Hewgill


Surely that code should be written like this?

gcold = gc.isenabled()
gc.disable()
try:
    timing = self.inner(it, self.timer)
finally:
    if gcold:
        gc.enable()

Otherwise garbage collection will remain disabled if the timed code throws an exception.

I reported this on bugs.python.org, and it was fixed in Python 2.7.3 and 3.2.2.

like image 38
Gareth Rees Avatar answered Oct 28 '22 20:10

Gareth Rees