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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With