-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
52 lines (40 loc) · 1.43 KB
/
benchmark.py
File metadata and controls
52 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import tkinter as tk
import timeit
import gc
def run_benchmark():
num_children = 100
num_iterations = 20000
root = tk.Tk()
frame1 = tk.Frame(root)
frame2 = tk.Frame(root)
for _ in range(num_children):
tk.Label(frame1, text="test").pack()
tk.Label(frame2, text="test").pack()
root.update_idletasks()
def bench_with_list():
# Only measure the overhead of the list allocation
children = list(frame1.winfo_children())
for c in children:
pass
def bench_without_list():
# Only measure without the list allocation
children = frame2.winfo_children()
for c in children:
pass
# Disable garbage collector for more stable timing
gc.disable()
# Warmup
for _ in range(100):
bench_with_list()
bench_without_list()
time_with_list = timeit.timeit(bench_with_list, number=num_iterations)
time_without_list = timeit.timeit(bench_without_list, number=num_iterations)
gc.enable()
print(f"Micro-benchmark results for iterating {num_children} children ({num_iterations} iterations):")
print(f"With list(): {time_with_list:.5f} seconds")
print(f"Without list(): {time_without_list:.5f} seconds")
improvement = ((time_with_list - time_without_list) / time_with_list) * 100
print(f"Improvement: {improvement:.2f}%")
root.destroy()
if __name__ == "__main__":
run_benchmark()