# Elapsed time debugging in gc.c

**URL:** <https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396>\
**Category:** Ideas\
**Tags:** GC\
**Created:** [October 16, 2025, 10:33am UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396 "2025-10-16T10:33:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgdr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pgdr/32/31836_2.png) [@pgdr](https://discuss.python.org/u/pgdr)\
**Post date:** [October 16, 2025, 10:33am UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/1 "2025-10-16T10:33:20Z")

</div>

In [GH-108362: Incremental GC implementation](https://github.com/python/cpython/pull/116206)#116206 (@markshannon ), it seems to me that the `elapsed time` debug information disappeared.

This change took effect in Python 3.14.

Is it possible to get back that piece of debug information?

In Python 3.13 with `gc.DEBUG_STATS` you would get information like this:

```python
gc: collecting generation 2...
gc: objects in each generation: 1473 0 5043
gc: objects in permanent generation: 0
gc: done, 0 unreachable, 0 uncollectable, 0.0236s elapsed

```

whereas in Python 3.14:

```python
gc: collecting generation 2...
gc: objects in each generation: 1394 5072 248
gc: objects in permanent generation: 0

```

Ps., I _believe_ that it is commit `1530932` that removes the elapsed time functionality, but I might be wrong.

---

<div class="post-metadata">

**Author:** ![sergey-miryanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sergey-miryanov/32/27486_2.png) [@sergey-miryanov](https://discuss.python.org/u/sergey-miryanov)\
**Post date:** [October 16, 2025, 7:55pm UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/2 "2025-10-16T19:55:14Z")

</div>

Hi @pdgr, do you want to implement this?

---

<div class="post-metadata">

**Author:** ![pgdr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pgdr/32/31836_2.png) [@pgdr](https://discuss.python.org/u/pgdr)\
**Post date:** [October 16, 2025, 9:03pm UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/3 "2025-10-16T21:03:44Z")

</div>

I can give it a shot.

---

<div class="post-metadata">

**Author:** ![pgdr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pgdr/32/31836_2.png) [@pgdr](https://discuss.python.org/u/pgdr)\
**Post date:** [October 19, 2025, 4:44pm UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/4 "2025-10-19T16:44:16Z")

</div>

Hi, Sergey. I have prepared a patch. Do you want to look at it first, or should I immediately create a PR and/or issue?

The gist of it is that we now

```python
#include "pycore_time.h" // _PyTime_MonotonicRaw()

```

in `gc.c`, and in `gc_collect_region`, create a `PyTime_t now`and at the end of that function we write it

```python
if (gcstate->debug & _PyGC_DEBUG_STATS) {
    PyTime_t endtime;
    (void)PyTime_MonotonicRaw(&endtime);
    double d = PyTime_AsSecondsDouble(endtime - now);
    PySys_WriteStderr(
        "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
        n+m, n, d
        );
}

```

Much of this is recovered from before the refactoring.

I’ve also added `Py_ssize_t m = 0;`that counts `unreachable` and `final_unreachable`.

---

<div class="post-metadata">

**Author:** ![sergey-miryanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sergey-miryanov/32/27486_2.png) [@sergey-miryanov](https://discuss.python.org/u/sergey-miryanov)\
**Post date:** [October 19, 2025, 7:57pm UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/5 "2025-10-19T19:57:53Z")

</div>

Hi, Pål!

It is fine if you just open PR (technically you have to create an issue for this and the PR that linked the issue, may be you find interesting to check [Python Developer’s Guide](https://devguide.python.org/)).

I’m not a codeowner for GC or core-dev, so I just can give you some suggestions, but last decision will be to core-devs.

First, you don’t need to introduce `m` variable you can use `stats→collected` and `stats→uncollectable`.

Second, I propose to add time measurements to `_PyGC_Collect` this will not be 100% equal to 3.13 version, but IMHO more appropriate here.

So, create the PR and let other devs review your solution. Feel free to ping me you have some questions.

Thanks!

---

<div class="post-metadata">

**Author:** ![pgdr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pgdr/32/31836_2.png) [@pgdr](https://discuss.python.org/u/pgdr)\
**Post date:** [October 21, 2025, 7:03am UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/6 "2025-10-21T07:03:57Z")

</div>

Issue [Bring back elapsed time debugging in gc.c · Issue #140358 · python/cpython · GitHub](https://github.com/python/cpython/issues/140358)

PR [https://github.com/python/cpython/pull/140359](https://github.com/python/cpython/pull/140359)

PR has been merged and issue closed.

Thanks for the assistance, Sergey!

---

<div class="post-metadata">

**Author:** ![sergey-miryanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sergey-miryanov/32/27486_2.png) [@sergey-miryanov](https://discuss.python.org/u/sergey-miryanov)\
**Post date:** [October 21, 2025, 7:45am UTC](https://discuss.python.org/t/elapsed-time-debugging-in-gc-c/104396/7 "2025-10-21T07:45:21Z")

</div>

Thanks for implementing this!
