January 8, 2024
A few weeks ago, I wrote the tool g203ctl. I reimplemented the functionality of the tool g203-led. Both let me change the LED effects on my mouse. However, the original script was written in Python. My version is written in Zig.
My version has a manpage and feels easier to use (at least for me).
Contrary to the Python version, mine never crashed so far. This is probably the
biggest reason that I reimplemented this tool. But maybe there was something wrong
with my setup when the Python implementation crashed, I can’t really tell.
Anyway, the final question remaining for me was: is it also faster?
To find out, I did a quick benchmark using Hyperfine: sudo hyperfine --warmup 3 'python3 g203-led.py lightsync breathe 0000ff 9000 42' 'g203ctl -l breathe 0000ff 9000 42'
.
I used the ReleaseSmall
optimization to build the g203ctl executable.
This is the result:
Benchmark 1: python3 g203-led.py lightsync breathe 0000ff 9000 42
Time (mean ± σ): 401.8 ms ± 2.2 ms [User: 96.5 ms, System: 101.1 ms]
Range (min … max): 398.2 ms … 405.8 ms 10 runs
Benchmark 2: g203ctl -l breathe 0000ff 9000 42
Time (mean ± σ): 384.4 ms ± 2.2 ms [User: 54.8 ms, System: 92.6 ms]
Range (min … max): 382.3 ms … 387.8 ms 10 runs
Summary
g203ctl -l breathe 0000ff 9000 42 ran
1.05 ± 0.01 times faster than python3 g203-led.py lightsync breathe 0000ff 9000 42
My Zig version is about 5% faster. Cool. Will I notice that? Definitely not. Most of the time is spent waiting for I/O stuff to complete. But it was/is a fun little project and I learned to write some Zig code.