This commit is contained in:
2026-01-22 01:59:03 +03:00
parent a174e0d5ab
commit 3fa7b17de9
+19 -24
View File
@@ -53,33 +53,28 @@ class Benchmark:
self.name = name self.name = name
self.description = description self.description = description
def single_run(self, is_gc_disabled: bool = False) -> float: def single_run(self) -> float:
if is_gc_disabled: with redirect_stdout(io.StringIO()):
was_gc_enabled = gc.isenabled() start = time.perf_counter()
gc.disable() self.func()
end = time.perf_counter()
with redirect_stdout(io.StringIO()): return (end - start) * MILLISECONDS_IN_SECONDS
start = time.perf_counter()
self.func()
end = time.perf_counter()
if was_gc_enabled:
gc.enable()
gc.collect()
return (end - start) * MILLISECONDS_IN_SECONDS
else:
with redirect_stdout(io.StringIO()):
start = time.perf_counter()
self.func()
end = time.perf_counter()
return (end - start) * MILLISECONDS_IN_SECONDS
def multiple_runs(self, iterations: int, is_gc_disabled: bool = False) -> tuple[float, ...]: def multiple_runs(self, iterations: int, is_gc_disabled: bool = False) -> tuple[float, ...]:
run_attempts: list[float] = [] run_attempts: list[float] = []
for _ in range(iterations): if is_gc_disabled:
run_attempts.append(self.single_run(is_gc_disabled)) was_gc_enabled = gc.isenabled()
return tuple(run_attempts) gc.disable()
for _ in range(iterations):
run_attempts.append(self.single_run())
if was_gc_enabled:
gc.enable()
gc.collect()
return tuple(run_attempts)
else:
for _ in range(iterations):
run_attempts.append(self.single_run())
return tuple(run_attempts)
@override @override
def __repr__(self) -> str: def __repr__(self) -> str: