Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "pyperformance_bm_gc_traversal"
requires-python = ">=3.8"
dependencies = ["pyperf"]
urls = {repository = "https://github.com/python/pyperformance"}
dynamic = ["version"]

[tool.pyperformance]
name = "gc_traversal"
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pyperf
import gc

N_LEVELS = 1000


def create_recursive_containers(n_levels):

current_list = []
for n in range(n_levels):
new_list = [None] * n
for index in range(n):
new_list[index] = current_list
current_list = new_list

return current_list


def benchamark_collection(loops, n_levels):
total_time = 0
all_cycles = create_recursive_containers(n_levels)
for _ in range(loops):
gc.collect()
# Main loop to measure
t0 = pyperf.perf_counter()
collected = gc.collect()
total_time += pyperf.perf_counter() - t0
Comment on lines +21 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure I understand, the GC objects aren't cleaned up during gc.collect() because all_cycles is still bound in the locals. That's why we can re-use it in each loop. Is that right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this test is just benchmarking the gc traversing all those objects again and again so no need to remove them


assert collected == 0

return total_time


if __name__ == "__main__":
runner = pyperf.Runner()
runner.metadata["description"] = "GC traversal benchmark"
runner.bench_time_func("gc_traversal", benchamark_collection, N_LEVELS)