1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "vere/pkg/c3/defs.h"
#include "vere/pkg/noun/allocate.h"
#include "vere/pkg/noun/manage.h"
#include "vere/pkg/noun/serial.h"
#include "vere/pkg/noun/v3/allocate.h"
// Timing utilities
static double get_time() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
// Benchmark a function
static void benchmark(const char* name, int iterations, void (*f)(void)) {
// Warmup
int warmup = iterations / 10;
if (warmup > 100) warmup = 100;
for (int i = 0; i < warmup; i++) {
f();
}
// Actual benchmark
double start = get_time();
for (int i = 0; i < iterations; i++) {
f();
}
double elapsed = get_time() - start;
double avg = elapsed / iterations;
printf("%-40s %d iters: avg=%.6f total=%.6f\n", name, iterations, avg, elapsed);
}
// Global variables for benchmarks
static u3_noun test_atom_small;
static u3_noun test_atom_large;
static u3_noun test_cell;
static u3_noun test_tree;
static c3_y* jam_small_bytes;
static c3_d jam_small_len;
static c3_y* jam_tree_bytes;
static c3_d jam_tree_len;
// Benchmark functions
static void bench_jam_cue_small() {
c3_d len;
c3_y* bytes;
u3s_jam_xeno(test_atom_small, &len, &bytes);
u3_noun result = u3s_cue_xeno(len, bytes);
free(bytes);
u3z(result);
}
static void bench_jam_cue_large() {
c3_d len;
c3_y* bytes;
u3s_jam_xeno(test_atom_large, &len, &bytes);
u3_noun result = u3s_cue_xeno(len, bytes);
free(bytes);
u3z(result);
}
static void bench_jam_cue_cell() {
c3_d len;
c3_y* bytes;
u3s_jam_xeno(test_cell, &len, &bytes);
u3_noun result = u3s_cue_xeno(len, bytes);
free(bytes);
u3z(result);
}
static void bench_jam_cue_tree() {
c3_d len;
c3_y* bytes;
u3s_jam_xeno(test_tree, &len, &bytes);
u3_noun result = u3s_cue_xeno(len, bytes);
free(bytes);
u3z(result);
}
static void bench_jam_only_small() {
c3_d len;
c3_y* bytes;
u3s_jam_xeno(test_atom_small, &len, &bytes);
free(bytes);
}
static void bench_cue_only_small() {
u3_noun result = u3s_cue_xeno(jam_small_len, jam_small_bytes);
u3z(result);
}
static void bench_jam_only_tree() {
c3_d len;
c3_y* bytes;
u3s_jam_xeno(test_tree, &len, &bytes);
free(bytes);
}
static void bench_cue_only_tree() {
u3_noun result = u3s_cue_xeno(jam_tree_len, jam_tree_bytes);
u3z(result);
}
int main(int argc, char* argv[]) {
// Initialize urbit runtime
c3_w wor_w = 1 << 27; // 128MB
u3m_boot_lite(wor_w);
// Create test data
test_atom_small = 42;
test_atom_large = u3i_chubs(1, (c3_d[]){1ULL << 63});
test_cell = u3nc(1, 2);
// Build balanced tree: [[1 2] [3 4]] [[5 6] [7 8]]
test_tree = u3nc(
u3nc(u3nc(1, 2), u3nc(3, 4)),
u3nc(u3nc(5, 6), u3nc(7, 8))
);
// Pre-jam for cue-only benchmarks
u3s_jam_xeno(test_atom_small, &jam_small_len, &jam_small_bytes);
u3s_jam_xeno(test_tree, &jam_tree_len, &jam_tree_bytes);
printf("========================================\n");
printf("Jam/Cue Serialization Benchmarks (C)\n");
printf("========================================\n\n");
printf("Round-trip benchmarks:\n");
benchmark("jam/cue small atom (42)", 100000, bench_jam_cue_small);
benchmark("jam/cue large atom (2^63)", 10000, bench_jam_cue_large);
benchmark("jam/cue simple cell [1 2]", 100000, bench_jam_cue_cell);
benchmark("jam/cue balanced tree (depth 3)", 50000, bench_jam_cue_tree);
printf("\nJam-only benchmarks:\n");
benchmark("jam only (small atom)", 100000, bench_jam_only_small);
benchmark("jam only (balanced tree)", 50000, bench_jam_only_tree);
printf("\nCue-only benchmarks:\n");
benchmark("cue only (small atom)", 100000, bench_cue_only_small);
benchmark("cue only (balanced tree)", 50000, bench_cue_only_tree);
printf("\n========================================\n");
// Cleanup
free(jam_small_bytes);
free(jam_tree_bytes);
return 0;
}
|