Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
180 lines (150 loc) · 5.32 KB
/
Copy pathbenchmark.py
File metadata and controls
180 lines (150 loc) · 5.32 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""
Performance benchmark for Conway's Game of Life with IPC
Tests sparse pattern (blinker) vs dense pattern (random 50% fill)
"""
import requests
import time
import json
BASE_URL = "http://localhost:8081"
def clear_grid():
"""Clear all cells"""
response = requests.post(f"{BASE_URL}/api/clear")
return response.json()
def set_cell(x, y, alive=True):
"""Set a single cell"""
response = requests.post(
f"{BASE_URL}/api/set",
json={"x": x, "y": y, "alive": 1 if alive else 0}
)
return response.json()
def tick():
"""Advance one generation"""
response = requests.post(f"{BASE_URL}/api/tick")
return response.json()
def get_state():
"""Get current grid state"""
response = requests.get(f"{BASE_URL}/api/state")
return response.json()
def benchmark_pattern(name, setup_func, num_ticks=100):
"""Benchmark a pattern"""
print(f"\n{'='*60}")
print(f"Benchmarking: {name}")
print(f"{'='*60}")
# Clear and setup
clear_grid()
alive_cells = setup_func()
print(f"Pattern setup: {alive_cells} alive cells")
# Get initial state
state = get_state()
print(f"Grid: {state['width']}x{state['height']} = {state['width']*state['height']} total cells")
print(f"Active cells: {len(state['cells'])}/{state['width']*state['height']} ({len(state['cells'])*100/(state['width']*state['height']):.1f}%)")
# Warmup
for _ in range(5):
tick()
# Benchmark
print(f"\nRunning {num_ticks} ticks...")
start = time.time()
for _ in range(num_ticks):
tick()
elapsed = time.time() - start
avg_tick_time = (elapsed / num_ticks) * 1000 # ms
ticks_per_sec = num_ticks / elapsed
print(f"Total time: {elapsed:.3f}s")
print(f"Average tick time: {avg_tick_time:.2f}ms")
print(f"Ticks per second: {ticks_per_sec:.1f}")
print(f"Generations per second: {ticks_per_sec:.1f}")
return {
"name": name,
"alive_cells": alive_cells,
"total_cells": state['width'] * state['height'],
"density": len(state['cells']) * 100 / (state['width'] * state['height']),
"total_time": elapsed,
"avg_tick_ms": avg_tick_time,
"ticks_per_sec": ticks_per_sec
}
def setup_blinker():
"""Setup a simple blinker (3 cells)"""
set_cell(5, 5)
set_cell(6, 5)
set_cell(7, 5)
return 3
def setup_glider():
"""Setup a glider (5 cells)"""
# Glider pattern
set_cell(5, 5)
set_cell(6, 6)
set_cell(4, 7)
set_cell(5, 7)
set_cell(6, 7)
return 5
def setup_sparse_random():
"""Setup sparse random pattern (~5% density)"""
import random
random.seed(42)
count = 0
for y in range(20):
for x in range(20):
if random.random() < 0.05:
set_cell(x, y)
count += 1
return count
def setup_medium_random():
"""Setup medium density random pattern (~25% density)"""
import random
random.seed(42)
count = 0
for y in range(20):
for x in range(20):
if random.random() < 0.25:
set_cell(x, y)
count += 1
return count
def setup_dense_random():
"""Setup dense random pattern (~50% density)"""
import random
random.seed(42)
count = 0
for y in range(20):
for x in range(20):
if random.random() < 0.5:
set_cell(x, y)
count += 1
return count
if __name__ == "__main__":
print("\n" + "="*60)
print("Conway's Game of Life - IPC Performance Benchmark")
print("="*60)
print("\nThis benchmark demonstrates the bounding box optimization.")
print("Sparse patterns should be much faster than dense patterns.")
results = []
# Test 1: Blinker (very sparse - 3 cells)
results.append(benchmark_pattern("Blinker (3 cells, ~0.75% density)", setup_blinker, num_ticks=100))
# Test 2: Glider (sparse - 5 cells)
results.append(benchmark_pattern("Glider (5 cells, ~1.25% density)", setup_glider, num_ticks=100))
# Test 3: Sparse random (~5% density)
results.append(benchmark_pattern("Sparse Random (~5% density)", setup_sparse_random, num_ticks=50))
# Test 4: Medium random (~25% density)
results.append(benchmark_pattern("Medium Random (~25% density)", setup_medium_random, num_ticks=50))
# Test 5: Dense random (~50% density)
results.append(benchmark_pattern("Dense Random (~50% density)", setup_dense_random, num_ticks=50))
# Summary
print("\n" + "="*60)
print("SUMMARY")
print("="*60)
print(f"{'Pattern':<35} {'Density':<10} {'ms/tick':<12} {'ticks/s':<10}")
print("-"*60)
for r in results:
print(f"{r['name']:<35} {r['density']:>6.1f}% {r['avg_tick_ms']:>8.2f}ms {r['ticks_per_sec']:>7.1f}")
print("\n" + "="*60)
print("OPTIMIZATION IMPACT")
print("="*60)
sparse = results[0] # Blinker
dense = results[-1] # Dense random
speedup = dense['avg_tick_ms'] / sparse['avg_tick_ms']
print(f"Sparse pattern (blinker): {sparse['avg_tick_ms']:.2f}ms per tick")
print(f"Dense pattern (50% fill): {dense['avg_tick_ms']:.2f}ms per tick")
print(f"Speedup ratio: {speedup:.1f}x faster for sparse patterns")
print("\nThe bounding box optimization checks only the active region,")
print("making sparse patterns much faster than dense patterns.")
print("="*60 + "\n")