Repository navigation
Expand file tree
/
Copy pathbenchmark_framebuf.py
More file actions
363 lines (295 loc) · 12.6 KB
/
Copy pathbenchmark_framebuf.py
File metadata and controls
363 lines (295 loc) · 12.6 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
Performance benchmark comparing C framebuf vs pure Python viper implementation
Tests common operations on realistic display sizes:
- MONO_VLSB 128x64 (SSD1306 OLED)
- RGB565 64x64 (Color display)
- GS8 128x128 (Grayscale display)
- MONO_HLSB 128x64 (Horizontal layout)
"""
import time
import framebuf
import framebuf_pure
# Global list to collect all benchmark results
results = []
# Helper function to measure execution time
def benchmark(func, iterations=100):
"""Run function multiple times and return average time in microseconds"""
start = time.ticks_us()
for _ in range(iterations):
func()
end = time.ticks_us()
elapsed = time.ticks_diff(end, start)
return elapsed / iterations
def format_time(us):
"""Format microseconds to readable string"""
if us < 1000:
return f"{us:.1f} µs"
elif us < 1000000:
return f"{us/1000:.1f} ms"
else:
return f"{us/1000000:.2f} s"
def benchmark_mono_vlsb():
"""Benchmark MONO_VLSB 128x64 (SSD1306 display)"""
print("\n" + "="*70)
print("MONO_VLSB 128x64 (SSD1306 OLED Display)")
print("="*70)
w, h = 128, 64
size = ((h + 7) // 8) * w # 1024 bytes
# Create buffers
buf_c = bytearray(size)
buf_py = bytearray(size)
fb_c = framebuf.FrameBuffer(buf_c, w, h, framebuf.MONO_VLSB)
fb_py = framebuf_pure.FrameBuffer(buf_py, w, h, framebuf_pure.MONO_VLSB)
print(f"Buffer size: {size} bytes\n")
# Benchmark fill
print("Operation: fill(1)")
time_c = benchmark(lambda: fb_c.fill(1), 100)
time_py = benchmark(lambda: fb_py.fill(1), 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_VLSB", "fill(1)", time_c, time_py, ratio))
# Benchmark horizontal line
print("\nOperation: hline(0, 32, 128, 1)")
time_c = benchmark(lambda: fb_c.hline(0, 32, 128, 1), 1000)
time_py = benchmark(lambda: fb_py.hline(0, 32, 128, 1), 1000)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_VLSB", "hline", time_c, time_py, ratio))
# Benchmark vertical line
print("\nOperation: vline(64, 0, 64, 1)")
time_c = benchmark(lambda: fb_c.vline(64, 0, 64, 1), 1000)
time_py = benchmark(lambda: fb_py.vline(64, 0, 64, 1), 1000)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_VLSB", "vline", time_c, time_py, ratio))
# Benchmark pixel set (1000 random pixels)
print("\nOperation: 100x pixel(x, y, 1) - scattered pixels")
def set_pixels_c():
for i in range(100):
fb_c.pixel((i * 37) % 128, (i * 23) % 64, 1)
def set_pixels_py():
for i in range(100):
fb_py.pixel((i * 37) % 128, (i * 23) % 64, 1)
time_c = benchmark(set_pixels_c, 100)
time_py = benchmark(set_pixels_py, 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_VLSB", "pixel_set", time_c, time_py, ratio))
# Benchmark pixel read
print("\nOperation: 100x pixel(x, y) - read pixels")
def read_pixels_c():
total = 0
for i in range(100):
total += fb_c.pixel((i * 37) % 128, (i * 23) % 64)
return total
def read_pixels_py():
total = 0
for i in range(100):
total += fb_py.pixel((i * 37) % 128, (i * 23) % 64)
return total
time_c = benchmark(read_pixels_c, 100)
time_py = benchmark(read_pixels_py, 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_VLSB", "pixel_get", time_c, time_py, ratio))
def benchmark_rgb565():
"""Benchmark RGB565 64x64 (Color display)"""
print("\n" + "="*70)
print("RGB565 64x64 (Color Display)")
print("="*70)
w, h = 64, 64
size = w * h * 2 # 8,192 bytes
# Create buffers
buf_c = bytearray(size)
buf_py = bytearray(size)
fb_c = framebuf.FrameBuffer(buf_c, w, h, framebuf.RGB565)
fb_py = framebuf_pure.FrameBuffer(buf_py, w, h, framebuf_pure.RGB565)
print(f"Buffer size: {size} bytes\n")
# Benchmark fill
print("Operation: fill(0xF800) - red")
time_c = benchmark(lambda: fb_c.fill(0xF800), 100)
time_py = benchmark(lambda: fb_py.fill(0xF800), 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("RGB565", "fill", time_c, time_py, ratio))
# Benchmark horizontal line
print("\nOperation: hline(0, 32, 64, 0x07E0) - green line")
time_c = benchmark(lambda: fb_c.hline(0, 32, 64, 0x07E0), 500)
time_py = benchmark(lambda: fb_py.hline(0, 32, 64, 0x07E0), 500)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("RGB565", "hline", time_c, time_py, ratio))
# Benchmark vertical line
print("\nOperation: vline(32, 0, 64, 0x001F) - blue line")
time_c = benchmark(lambda: fb_c.vline(32, 0, 64, 0x001F), 500)
time_py = benchmark(lambda: fb_py.vline(32, 0, 64, 0x001F), 500)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("RGB565", "vline", time_c, time_py, ratio))
# Benchmark pixel operations
print("\nOperation: 100x pixel(x, y, color) - scattered pixels")
def set_pixels_c():
for i in range(100):
fb_c.pixel((i * 37) % 64, (i * 47) % 64, 0xFFFF)
def set_pixels_py():
for i in range(100):
fb_py.pixel((i * 37) % 64, (i * 47) % 64, 0xFFFF)
time_c = benchmark(set_pixels_c, 100)
time_py = benchmark(set_pixels_py, 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("RGB565", "pixel_set", time_c, time_py, ratio))
def benchmark_gs8():
"""Benchmark GS8 128x128 (Grayscale display)"""
print("\n" + "="*70)
print("GS8 128x128 (8-bit Grayscale Display)")
print("="*70)
w, h = 128, 128
size = w * h # 16,384 bytes
# Create buffers
buf_c = bytearray(size)
buf_py = bytearray(size)
fb_c = framebuf.FrameBuffer(buf_c, w, h, framebuf.GS8)
fb_py = framebuf_pure.FrameBuffer(buf_py, w, h, framebuf_pure.GS8)
print(f"Buffer size: {size} bytes\n")
# Benchmark fill
print("Operation: fill(128)")
time_c = benchmark(lambda: fb_c.fill(128), 50)
time_py = benchmark(lambda: fb_py.fill(128), 50)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("GS8", "fill", time_c, time_py, ratio))
# Benchmark horizontal line
print("\nOperation: hline(0, 64, 128, 255)")
time_c = benchmark(lambda: fb_c.hline(0, 64, 128, 255), 500)
time_py = benchmark(lambda: fb_py.hline(0, 64, 128, 255), 500)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("GS8", "hline", time_c, time_py, ratio))
# Benchmark vertical line
print("\nOperation: vline(64, 0, 128, 200)")
time_c = benchmark(lambda: fb_c.vline(64, 0, 128, 200), 500)
time_py = benchmark(lambda: fb_py.vline(64, 0, 128, 200), 500)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("GS8", "vline", time_c, time_py, ratio))
# Benchmark pixel operations
print("\nOperation: 100x pixel(x, y, gray) - scattered pixels")
def set_pixels_c():
for i in range(100):
fb_c.pixel((i * 67) % 128, (i * 97) % 128, i % 256)
def set_pixels_py():
for i in range(100):
fb_py.pixel((i * 67) % 128, (i * 97) % 128, i % 256)
time_c = benchmark(set_pixels_c, 100)
time_py = benchmark(set_pixels_py, 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("GS8", "pixel_set", time_c, time_py, ratio))
def benchmark_mono_hlsb():
"""Benchmark MONO_HLSB 128x64"""
print("\n" + "="*70)
print("MONO_HLSB 128x64 (Horizontal layout)")
print("="*70)
w, h = 128, 64
size = ((w + 7) // 8) * h # 1024 bytes
# Create buffers
buf_c = bytearray(size)
buf_py = bytearray(size)
fb_c = framebuf.FrameBuffer(buf_c, w, h, framebuf.MONO_HLSB)
fb_py = framebuf_pure.FrameBuffer(buf_py, w, h, framebuf_pure.MONO_HLSB)
print(f"Buffer size: {size} bytes\n")
# Benchmark fill
print("Operation: fill(1)")
time_c = benchmark(lambda: fb_c.fill(1), 100)
time_py = benchmark(lambda: fb_py.fill(1), 100)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_HLSB", "fill(1)", time_c, time_py, ratio))
# Benchmark horizontal line (byte-spanning case - most complex)
print("\nOperation: hline(5, 32, 118, 1) - byte spanning")
time_c = benchmark(lambda: fb_c.hline(5, 32, 118, 1), 1000)
time_py = benchmark(lambda: fb_py.hline(5, 32, 118, 1), 1000)
ratio = time_py / time_c
print(f" C impl: {format_time(time_c)}")
print(f" Viper impl: {format_time(time_py)}")
print(f" Ratio: {ratio:.2f}x {'slower' if ratio > 1 else 'faster'}")
results.append(("MONO_HLSB", "hline", time_c, time_py, ratio))
def print_summary_table():
"""Print summary table of all benchmark results"""
print("\n" + "="*70)
print("PERFORMANCE SUMMARY TABLE")
print("="*70)
print(f"{'Format':<12} {'Operation':<12} {'C (µs)':<12} {'Viper (µs)':<12} {'Ratio':>8}")
print("-"*70)
for fmt, op, time_c, time_py, ratio in results:
c_str = f"{time_c:.1f}" if time_c < 1000 else f"{time_c/1000:.1f}ms"
py_str = f"{time_py:.1f}" if time_py < 1000 else f"{time_py/1000:.1f}ms"
ratio_str = f"{ratio:.2f}x"
print(f"{fmt:<12} {op:<12} {c_str:<12} {py_str:<12} {ratio_str:>8}")
print("="*70)
print("\nKey:")
print(" Ratio < 1.0 = Viper is FASTER than C")
print(" Ratio > 1.0 = Viper is SLOWER than C")
print("="*70)
def run_all_benchmarks():
"""Run all benchmark suites"""
global results
results = [] # Reset results list
print("\n" + "="*70)
print("MicroPython FrameBuf Performance Benchmark")
print("Comparing C implementation vs Pure Python Viper implementation")
print("="*70)
# Run benchmarks for each format
benchmark_mono_vlsb()
# RGB565 with memory-safe size
try:
benchmark_rgb565()
except MemoryError:
print("\n⚠ Skipping RGB565 64x64 - insufficient memory")
try:
benchmark_gs8()
except MemoryError:
print("\n⚠ Skipping GS8 128x128 - insufficient memory")
benchmark_mono_hlsb()
# Print summary table
print_summary_table()
print("\n" + "="*70)
print("Benchmark Complete")
print("="*70)
print("\nNotes:")
print("- Lower ratio is better (closer to C performance)")
print("- Viper optimizations significantly improve pure Python speed")
print("- Fill operations are memory-bandwidth limited")
print("- Line operations benefit from loop optimization")
print("="*70)
if __name__ == "__main__":
run_all_benchmarks()