Skip to content

Commit b6b4dd0

Browse files
committed
Add Clang builds and CI testing for both GCC and Clang
- Makefile now builds with both GCC and Clang (C17 and C23) - Detects real GCC vs Apple's clang alias on macOS - CI runs separate jobs for GCC, Clang, and Zig on Ubuntu - Updated benchmark script to test all 5 versions - Updated BENCHMARK.md with comprehensive results
1 parent 1ad44b9 commit b6b4dd0

File tree

4 files changed

+215
-141
lines changed

4 files changed

+215
-141
lines changed

.github/workflows/zig.yml

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ on:
77
branches: [ main, zig-port ]
88

99
jobs:
10-
c-build:
10+
c-build-gcc:
11+
name: C Build (GCC)
1112
runs-on: ubuntu-latest
1213

1314
steps:
@@ -16,21 +17,45 @@ jobs:
1617
- name: Check GCC version
1718
run: gcc --version
1819

19-
- name: Build C versions (C17 and C23)
20-
run: cd c && make clean && make
20+
- name: Build with GCC (C17 and C23)
21+
run: cd c && make clean && make gcc-builds
2122

22-
- name: Test C17 executable runs
23-
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat-c17 || true
23+
- name: Test GCC C17 executable
24+
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat-gcc-c17 || true
2425

25-
- name: Test C23 executable runs
26-
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat-c23 || true
26+
- name: Test GCC C23 executable
27+
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat-gcc-c23 || true
2728

28-
- name: Compare binary sizes
29-
run: |
30-
echo "Binary sizes:"
31-
ls -lh c/chat-c17 c/chat-c23
29+
- name: Show binary sizes
30+
run: ls -lh c/chat-gcc-*
31+
32+
c-build-clang:
33+
name: C Build (Clang)
34+
runs-on: ubuntu-latest
35+
36+
steps:
37+
- uses: actions/checkout@v4
38+
39+
- name: Install Clang
40+
run: sudo apt-get update && sudo apt-get install -y clang
41+
42+
- name: Check Clang version
43+
run: clang --version
44+
45+
- name: Build with Clang (C17 and C23)
46+
run: cd c && make clean && make clang-builds
47+
48+
- name: Test Clang C17 executable
49+
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat-clang-c17 || true
50+
51+
- name: Test Clang C23 executable
52+
run: cd c && echo -e "hi\npython\nexit" | timeout 5 ./chat-clang-c23 || true
53+
54+
- name: Show binary sizes
55+
run: ls -lh c/chat-clang-*
3256

3357
zig-build-and-test:
58+
name: Zig Build & Test
3459
runs-on: ubuntu-latest
3560

3661
steps:

BENCHMARK.md

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,82 @@
11
# Chatbot Build & Runtime Benchmark Results
22

3-
Benchmark comparing C (C17 and C23 standards) and Zig implementations of the chatbot.
3+
Benchmark comparing C (GCC and Clang, C17 and C23 standards) and Zig implementations.
44

55
**System:** macOS arm64
66
**Date:** 2026-01-12
77

8-
## Build Results
8+
## Compiler Versions (Local)
99

10-
| Metric | C17 | C23 | Zig | Notes |
11-
|--------|-----|-----|-----|-------|
12-
| Build Time | ~90ms | ~90ms | ~710ms | Zig ~8x slower |
13-
| Executable Size | 33K | 33K | 1.3M | Zig ~40x larger |
10+
| Compiler | Version |
11+
|----------|---------|
12+
| GCC (macOS) | Apple Clang 17.0.0 (gcc is aliased to clang) |
13+
| Clang | Homebrew Clang 21.1.8 |
14+
| Zig | 0.15.2 |
1415

1516
## Runtime Results
1617

1718
| Version | Execution Time | Relative |
1819
|---------|---------------|----------|
19-
| C17 | 147ms | 1.00x |
20-
| C23 | 197ms | 1.34x |
21-
| Zig | 459ms | 3.12x |
20+
| GCC C17 | 228ms | 2.23x |
21+
| GCC C23 | 102ms | 1.00x (fastest) |
22+
| Clang C17 | 107ms | 1.04x |
23+
| Clang C23 | 106ms | 1.03x |
24+
| Zig | 628ms | 6.15x |
25+
26+
## Binary Sizes
27+
28+
| Version | Size |
29+
|---------|------|
30+
| GCC C17 | 33K |
31+
| GCC C23 | 33K |
32+
| Clang C17 | 33K |
33+
| Clang C23 | 33K |
34+
| Zig | 1.3M |
2235

2336
## Analysis
2437

25-
### Build Time
26-
- **C (C17/C23)**: Fast compilation using GCC with minimal optimization
27-
- **Zig**: Longer compilation time due to more comprehensive compiler
38+
### C Compilers
39+
- **GCC C23** was fastest in this run (times vary between runs)
40+
- **Clang** produces consistently fast binaries across C17/C23
41+
- On macOS, `gcc` is actually Apple Clang; real GCC is tested on CI (Ubuntu)
2842

29-
### Executable Size
30-
- **C (33K)**: Small, minimal runtime, links against system libc
31-
- **Zig (1.3M)**: Larger due to embedded standard library and runtime
32-
33-
### Runtime Performance
34-
- **C17** is the fastest, likely due to mature compiler optimizations
35-
- **C23** is slightly slower (may vary based on compiler version)
36-
- **Zig** is slower due to:
43+
### Zig
44+
- Slower due to:
3745
- GeneralPurposeAllocator overhead vs C's stack allocation
38-
- New Zig 0.15 buffered I/O system overhead
39-
- Additional safety checks
46+
- Zig 0.15 buffered I/O system overhead
47+
- Additional runtime safety checks
48+
- Much larger binary (embeds stdlib, no libc dependency)
49+
50+
### Binary Size
51+
- All C versions: 33K (links against system libc)
52+
- Zig: 1.3M (self-contained, no external dependencies)
4053

4154
## Build Configuration
4255

43-
- **C17**: `gcc -std=c17 -Wall -Wextra -pedantic`
44-
- **C23**: `gcc -std=c23 -Wall -Wextra -pedantic` (falls back to `-std=c2x` on older compilers)
45-
- **Zig**: `zig build` (debug mode, Zig 0.15.2)
56+
| Compiler | Flags |
57+
|----------|-------|
58+
| GCC C17 | `gcc -std=c17 -Wall -Wextra -pedantic` |
59+
| GCC C23 | `gcc -std=c23 -Wall -Wextra -pedantic` |
60+
| Clang C17 | `clang -std=c17 -Wall -Wextra -pedantic` |
61+
| Clang C23 | `clang -std=c23 -Wall -Wextra -pedantic` |
62+
| Zig | `zig build` (debug mode) |
4663

4764
## Notes
4865

4966
- All versions produce identical output
50-
- Times are from cold builds (no cache)
51-
- Runtime measured with same test input file
52-
- Zig uses pure Zig I/O (no libc linking)
67+
- C23 falls back to `-std=c2x` on older compilers
68+
- Times vary between runs; relative performance is more meaningful
69+
- CI tests both GCC and Clang on Ubuntu
5370

5471
## Running the Benchmarks
5572

5673
```bash
57-
# Full runtime benchmark
74+
# Full runtime benchmark (all compilers)
5875
./run_benchmarks.sh
5976

6077
# Build-only benchmark
6178
./benchmark.sh
79+
80+
# Show detected compilers
81+
cd c && make info
6282
```

c/Makefile

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,56 @@
1-
CC = gcc
1+
# Detect real GCC vs Apple's gcc alias
2+
UNAME_S := $(shell uname -s)
3+
ifeq ($(UNAME_S),Darwin)
4+
# macOS: gcc is actually clang, use explicit paths if available
5+
GCC := $(shell which gcc-14 gcc-13 gcc-12 2>/dev/null | head -1)
6+
ifeq ($(GCC),)
7+
GCC := gcc
8+
GCC_NOTE := "(Apple Clang)"
9+
endif
10+
else
11+
GCC := gcc
12+
endif
13+
14+
CLANG := clang
215
CFLAGS_COMMON = -Wall -Wextra -pedantic
316
SRC = src/chatbot.c
417

5-
all: chat-c17 chat-c23
18+
# Default targets
19+
all: gcc-builds clang-builds
20+
21+
# GCC builds
22+
gcc-builds: chat-gcc-c17 chat-gcc-c23
23+
24+
chat-gcc-c17:
25+
$(GCC) -std=c17 $(CFLAGS_COMMON) $(SRC) -o chat-gcc-c17
26+
27+
chat-gcc-c23:
28+
$(GCC) -std=c23 $(CFLAGS_COMMON) $(SRC) -o chat-gcc-c23 2>/dev/null || \
29+
$(GCC) -std=c2x $(CFLAGS_COMMON) $(SRC) -o chat-gcc-c23
630

7-
chat-c17:
8-
$(CC) -std=c17 $(CFLAGS_COMMON) $(SRC) -o chat-c17
31+
# Clang builds
32+
clang-builds: chat-clang-c17 chat-clang-c23
933

10-
chat-c23:
11-
$(CC) -std=c23 $(CFLAGS_COMMON) $(SRC) -o chat-c23 2>/dev/null || \
12-
$(CC) -std=c2x $(CFLAGS_COMMON) $(SRC) -o chat-c23
34+
chat-clang-c17:
35+
$(CLANG) -std=c17 $(CFLAGS_COMMON) $(SRC) -o chat-clang-c17
1336

14-
# Legacy target for backwards compatibility
15-
chat: chat-c17
16-
cp chat-c17 chat
37+
chat-clang-c23:
38+
$(CLANG) -std=c23 $(CFLAGS_COMMON) $(SRC) -o chat-clang-c23 2>/dev/null || \
39+
$(CLANG) -std=c2x $(CFLAGS_COMMON) $(SRC) -o chat-clang-c23
40+
41+
# Legacy targets for backwards compatibility
42+
chat: chat-gcc-c17
43+
cp chat-gcc-c17 chat
44+
45+
chat-c17: chat-gcc-c17
46+
cp chat-gcc-c17 chat-c17
47+
48+
chat-c23: chat-gcc-c23
49+
cp chat-gcc-c23 chat-c23
1750

1851
clean:
19-
rm -rf *.o chat chat-c17 chat-c23
52+
rm -rf *.o chat chat-c17 chat-c23 chat-gcc-* chat-clang-*
53+
54+
info:
55+
@echo "GCC: $(GCC) $(GCC_NOTE)"
56+
@echo "Clang: $(CLANG)"

0 commit comments

Comments
 (0)