Skip to content

Commit 1d093b6

Browse files
authored
Merge pull request #8 from gotchacode/zig-port
Port chatbot from C to Zig with comprehensive benchmarking
2 parents a468483 + e2c086d commit 1d093b6

File tree

17 files changed

+695
-23
lines changed

17 files changed

+695
-23
lines changed

.github/workflows/zig.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Build & Tests
2+
3+
on:
4+
push:
5+
branches: [ main, zig-port ]
6+
pull_request:
7+
branches: [ main, zig-port ]
8+
9+
jobs:
10+
c-build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Build C version
17+
run: cd c && make clean && make
18+
19+
- name: Test C executable runs
20+
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat || true
21+
22+
zig-build-and-test:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- uses: actions/checkout@v4
27+
28+
- uses: goto-bus-stop/setup-zig@v2
29+
with:
30+
version: 0.15.2
31+
32+
- name: Build Zig chatbot
33+
run: cd zig && mkdir -p zig-out/bin && zig build-exe src/main.zig -femit-bin=zig-out/bin/chat
34+
35+
- name: Run Zig tests
36+
run: cd zig && zig test src/chatbot.zig
37+
38+
- name: Test Zig executable runs
39+
run: cd zig && echo -e "hi\npython\nexit" | timeout 5 ./zig-out/bin/chat || true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ chat
2323
*.i*86
2424
*.x86_64
2525
*.hex
26+
27+
# Zig build artifacts
28+
.zig-cache/
29+
zig-out/

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}"

Makefile renamed to c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ all:
22
gcc -std=c11 -Wall -Wextra -pedantic src/chatbot.c -o chat
33

44
clean:
5-
rm -rf *o chat
5+
rm -rf *.o chat
File renamed without changes.
File renamed without changes.

example.c

Whitespace-only changes.

readme.md

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,81 @@
11
# Chatbot
22

3-
[![Build Status](https://travis-ci.org/vinitkumar/chatbot.svg?branch=master)](https://travis-ci.org/vinitkumar/chatbot)
3+
A simple keyword-based chatbot with implementations in both C and Zig.
44

5-
Chatbot is a simple bot written in C. The base algorithm is that our conversation is based on keywords. The bot seaches for this keyword and present a suitable response on basis of that keyword.
5+
## Benchmarks
66

7-
## Installation
7+
### Build Time & Size
88

9-
Installation is very easy via `MakeFile` :
9+
See [BENCHMARK.md](BENCHMARK.md) for detailed build metrics.
1010

11-
Just run:
11+
| Metric | C | Zig | Difference |
12+
|--------|---|-----|-----------|
13+
| Build Time | 93ms | 710ms | Zig is 7.63x slower |
14+
| Executable Size | 33K | 1.3M | Zig is 39x larger |
1215

16+
Run build benchmark:
17+
```bash
18+
./benchmark.sh
1319
```
14-
chatbot ➤ make
15-
gcc src/chatbot.c -o chat
20+
21+
### Runtime Performance
22+
23+
Both versions produce identical output. Runtime measurements with test inputs:
24+
25+
| Metric | C | Zig | Difference |
26+
|--------|---|-----|-----------|
27+
| Execution Time | 286ms | 526ms | C is 1.83x faster |
28+
29+
Run runtime benchmark:
30+
```bash
31+
./run_benchmarks.sh
1632
```
1733

18-
## Usage
34+
## Building
35+
36+
### C Version
1937

20-
After running `make all` you get a executable named as `chat`. Now run `./chat` and start chatting with the bot.
38+
```bash
39+
cd c
40+
make
41+
./chat
42+
```
43+
44+
See `c/Makefile` for more details.
2145

22-
## Demo
46+
### Zig Version
2347

48+
```bash
49+
cd zig
50+
zig build-exe src/main.zig -femit-bin=zig-out/bin/chat
51+
./zig-out/bin/chat
2452
```
25-
chatbot ➤ ./chat
26-
$ Chatbot v1.0.0!
2753

28-
$ (user) hi
54+
Or use the Zig build system:
2955

30-
$ (chatbot) hello
56+
```bash
57+
cd zig
58+
zig build run
59+
```
3160

32-
$ (user) python
61+
See `zig/README.md` for more details.
3362

34-
$ (chatbot) Yo, I love Python
63+
## Testing
3564

36-
$ (user) What
65+
### C Version
3766

38-
$ (chatbot) It is clear, ain't it?
67+
No automated tests (original implementation).
3968

40-
$ (user) exit
69+
### Zig Version
70+
71+
```bash
72+
cd zig
73+
zig test src/chatbot.zig
4174
```
4275

43-
## Issues and Pull request.
76+
Or:
4477

45-
Feel free to create an issue if you notice a bug. Pull request are really
46-
welcome with good fixes.
78+
```bash
79+
cd zig
80+
zig build test
81+
```

run_benchmarks.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "=========================================="
6+
echo "Chatbot Runtime Benchmark & Testing"
7+
echo "=========================================="
8+
echo ""
9+
10+
# Build both versions
11+
echo "Building both versions..."
12+
echo ""
13+
14+
cd c
15+
make clean > /dev/null 2>&1
16+
make > /dev/null 2>&1
17+
cd ..
18+
19+
cd zig
20+
rm -rf zig-out .zig-cache > /dev/null 2>&1
21+
mkdir -p zig-out/bin
22+
zig build-exe src/main.zig -femit-bin=zig-out/bin/chat > /dev/null 2>&1
23+
cd ..
24+
25+
echo "Build complete!"
26+
echo ""
27+
28+
# Test C version
29+
echo "=========================================="
30+
echo "C Version Testing & Benchmarking"
31+
echo "=========================================="
32+
echo ""
33+
echo "Running with test inputs..."
34+
35+
C_START=$(date +%s%N)
36+
c/./chat < test_inputs.txt > /tmp/c_output.txt 2>&1
37+
C_END=$(date +%s%N)
38+
C_TIME=$(( (C_END - C_START) / 1000000 )) # milliseconds
39+
40+
echo "Output:"
41+
cat /tmp/c_output.txt
42+
echo ""
43+
echo "Execution time: ${C_TIME}ms"
44+
echo ""
45+
46+
# Test Zig version
47+
echo "=========================================="
48+
echo "Zig Version Testing & Benchmarking"
49+
echo "=========================================="
50+
echo ""
51+
echo "Running with test inputs..."
52+
53+
ZIG_START=$(date +%s%N)
54+
zig/zig-out/bin/./chat < test_inputs.txt > /tmp/zig_output.txt 2>&1
55+
ZIG_END=$(date +%s%N)
56+
ZIG_TIME=$(( (ZIG_END - ZIG_START) / 1000000 )) # milliseconds
57+
58+
echo "Output:"
59+
cat /tmp/zig_output.txt
60+
echo ""
61+
echo "Execution time: ${ZIG_TIME}ms"
62+
echo ""
63+
64+
# Compare outputs
65+
echo "=========================================="
66+
echo "Output Comparison"
67+
echo "=========================================="
68+
echo ""
69+
70+
if diff -q /tmp/c_output.txt /tmp/zig_output.txt > /dev/null 2>&1; then
71+
echo "✓ Outputs are identical"
72+
else
73+
echo "✗ Outputs differ"
74+
echo ""
75+
echo "C output:"
76+
cat /tmp/c_output.txt
77+
echo ""
78+
echo "Zig output:"
79+
cat /tmp/zig_output.txt
80+
fi
81+
82+
echo ""
83+
84+
# Runtime comparison
85+
echo "=========================================="
86+
echo "Runtime Comparison"
87+
echo "=========================================="
88+
echo ""
89+
90+
if [ $C_TIME -lt $ZIG_TIME ]; then
91+
RATIO=$(echo "scale=2; $ZIG_TIME / $C_TIME" | bc)
92+
echo "C version is ${RATIO}x faster"
93+
else
94+
RATIO=$(echo "scale=2; $C_TIME / $ZIG_TIME" | bc)
95+
echo "Zig version is ${RATIO}x faster"
96+
fi
97+
98+
echo ""
99+
echo "Summary:"
100+
echo " C: ${C_TIME}ms"
101+
echo " Zig: ${ZIG_TIME}ms"
102+
echo ""

0 commit comments

Comments
 (0)