Files
lux/benchmarks/binarytrees.c
Brandon Lucas 42fef80a47 feat: add comprehensive benchmark suite with multi-language comparison
Add benchmarks comparing Lux against 7 languages:
- Rust, C, Go (compiled)
- Node.js, Bun (JavaScript JIT)
- Python (interpreted)

Benchmarks:
- Fibonacci (fib 35): recursive function calls
- Prime counting (10k): loops and conditionals
- Sum loop (10M): tight numeric loops
- Ackermann (3,10): deep recursion
- Selection sort (1k): sorting algorithm
- List operations (10k): map/filter/fold with closures

Results show Lux:
- Matches C and Rust performance
- 2-5x faster than Go
- 7-15x faster than Node.js
- 10-285x faster than Python

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-14 16:17:06 -05:00

62 lines
1.4 KiB
C

// Binary Trees benchmark - recursive data structures
#include <stdio.h>
#include <stdlib.h>
typedef struct Tree {
struct Tree* left;
struct Tree* right;
} Tree;
Tree* make(int depth) {
Tree* t = malloc(sizeof(Tree));
if (depth == 0) {
t->left = NULL;
t->right = NULL;
} else {
t->left = make(depth - 1);
t->right = make(depth - 1);
}
return t;
}
long check(Tree* t) {
if (t->left == NULL) return 1;
return 1 + check(t->left) + check(t->right);
}
void free_tree(Tree* t) {
if (t->left) free_tree(t->left);
if (t->right) free_tree(t->right);
free(t);
}
int main() {
int minDepth = 4;
int maxDepth = 14;
// Stretch tree
int stretchDepth = maxDepth + 1;
Tree* stretchTree = make(stretchDepth);
printf("stretch tree check: %ld\n", check(stretchTree));
free_tree(stretchTree);
// Long lived tree
Tree* longLivedTree = make(maxDepth);
// Iterate through depths
for (int depth = minDepth; depth <= maxDepth; depth += 2) {
int iterations = 1 << (maxDepth - depth + 4);
long sum = 0;
for (int i = 0; i < iterations; i++) {
Tree* t = make(depth);
sum += check(t);
free_tree(t);
}
printf("%d trees of depth %d check: %ld\n", iterations, depth, sum);
}
printf("long lived tree check: %ld\n", check(longLivedTree));
free_tree(longLivedTree);
return 0;
}