Skip to content

Commit a8d9a6b

Browse files
committed
Add build benchmark script and results
1 parent 4664b0b commit a8d9a6b

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

BENCHMARK.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Chatbot Build Benchmark Results
2+
3+
Benchmark comparing C and Zig implementations of the chatbot.
4+
5+
**System:** macOS arm64
6+
**Date:** 2026-01-12
7+
8+
## Results
9+
10+
| Metric | C | Zig | Difference |
11+
|--------|---|-----|-----------|
12+
| Build Time | 93ms | 710ms | Zig is 7.63x slower |
13+
| Executable Size | 33K | 1.3M | Zig is 39x larger |
14+
15+
## Analysis
16+
17+
### Build Time
18+
- **C (93ms)**: Fast compilation using GCC with minimal optimization
19+
- **Zig (710ms)**: Longer compilation time due to Zig's more comprehensive compiler
20+
21+
The C version compiles significantly faster due to:
22+
- Simpler compilation pipeline
23+
- No build system overhead (direct gcc command)
24+
- Minimal type checking and analysis
25+
26+
### Executable Size
27+
- **C (33K)**: Small, minimal runtime
28+
- **Zig (1.3M)**: Larger due to Zig's standard library and runtime
29+
30+
The Zig executable is larger because:
31+
- Zig stdlib is embedded in the binary
32+
- More comprehensive runtime features
33+
- GeneralPurposeAllocator adds overhead
34+
35+
## Notes
36+
37+
- Both versions are unoptimized builds
38+
- C build uses `-std=c11 -Wall -Wextra -pedantic`
39+
- Zig build uses default optimization level
40+
- Times are from cold builds (no cache)
41+
42+
## Running the Benchmark
43+
44+
```bash
45+
./benchmark.sh
46+
```
47+
48+
This will clean, rebuild both versions, and compare build times and sizes.

benchmark.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "=========================================="
6+
echo "Chatbot Build Benchmark"
7+
echo "=========================================="
8+
echo ""
9+
10+
# C Version Benchmark
11+
echo "C Version Benchmark"
12+
echo "------------------------------------------"
13+
14+
cd c
15+
echo "Cleaning..."
16+
make clean > /dev/null 2>&1
17+
18+
echo "Building C version..."
19+
C_START=$(date +%s%N)
20+
make > /dev/null 2>&1
21+
C_END=$(date +%s%N)
22+
C_TIME=$(( (C_END - C_START) / 1000000 )) # Convert to milliseconds
23+
24+
C_SIZE=$(ls -lh chat | awk '{print $5}')
25+
26+
echo "Build time: ${C_TIME}ms"
27+
echo "Executable size: ${C_SIZE}"
28+
29+
cd ..
30+
echo ""
31+
32+
# Zig Version Benchmark
33+
echo "Zig Version Benchmark"
34+
echo "------------------------------------------"
35+
36+
cd zig
37+
echo "Cleaning..."
38+
rm -rf zig-out .zig-cache > /dev/null 2>&1
39+
mkdir -p zig-out/bin
40+
41+
echo "Building Zig version..."
42+
ZIG_START=$(date +%s%N)
43+
zig build-exe src/main.zig -femit-bin=zig-out/bin/chat > /dev/null 2>&1
44+
ZIG_END=$(date +%s%N)
45+
ZIG_TIME=$(( (ZIG_END - ZIG_START) / 1000000 )) # Convert to milliseconds
46+
47+
ZIG_SIZE=$(ls -lh zig-out/bin/chat | awk '{print $5}')
48+
49+
echo "Build time: ${ZIG_TIME}ms"
50+
echo "Executable size: ${ZIG_SIZE}"
51+
52+
cd ..
53+
echo ""
54+
55+
# Comparison
56+
echo "Comparison"
57+
echo "------------------------------------------"
58+
if [ $C_TIME -lt $ZIG_TIME ]; then
59+
RATIO=$(echo "scale=2; $ZIG_TIME / $C_TIME" | bc)
60+
echo "C version is ${RATIO}x faster"
61+
else
62+
RATIO=$(echo "scale=2; $C_TIME / $ZIG_TIME" | bc)
63+
echo "Zig version is ${RATIO}x faster"
64+
fi
65+
66+
echo ""
67+
echo "Summary:"
68+
echo " C: ${C_TIME}ms, ${C_SIZE}"
69+
echo " Zig: ${ZIG_TIME}ms, ${ZIG_SIZE}"

0 commit comments

Comments
 (0)