Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit e21ce53

Browse files
committed
examples
1 parent 3d9a0ee commit e21ce53

File tree

4 files changed

+1002
-0
lines changed

4 files changed

+1002
-0
lines changed

examples/jit_benchmark_test.cn

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
// CodeNothing v0.7.7 JIT编译器基准测试
2+
using lib <io>;
3+
4+
fn benchmark_simple_loop(): int {
5+
print("📊 基准测试: 简单循环");
6+
7+
// 大量迭代的简单循环,测试JIT编译效果
8+
sum : int = 0;
9+
for (i : 1..1000) {
10+
sum = sum + i;
11+
};
12+
13+
print("简单循环基准测试结果: " + sum);
14+
return sum;
15+
};
16+
17+
fn benchmark_arithmetic_intensive(): int {
18+
print("📊 基准测试: 算术密集型循环");
19+
20+
// 算术运算密集的循环
21+
result : int = 0;
22+
for (i : 1..500) {
23+
temp : int = i * i + i * 2 + i / 2;
24+
result = result + temp;
25+
};
26+
27+
print("算术密集型基准测试结果: " + result);
28+
return result;
29+
};
30+
31+
fn benchmark_conditional_branches(): int {
32+
print("📊 基准测试: 条件分支循环");
33+
34+
// 包含大量条件分支的循环
35+
branch_sum : int = 0;
36+
for (i : 1..300) {
37+
if (i % 10 == 0) {
38+
branch_sum = branch_sum + i * 5;
39+
} else {
40+
if (i % 5 == 0) {
41+
branch_sum = branch_sum + i * 3;
42+
} else {
43+
if (i % 2 == 0) {
44+
branch_sum = branch_sum + i * 2;
45+
} else {
46+
branch_sum = branch_sum + i;
47+
};
48+
};
49+
};
50+
};
51+
52+
print("条件分支基准测试结果: " + branch_sum);
53+
return branch_sum;
54+
};
55+
56+
fn benchmark_nested_loops(): int {
57+
print("📊 基准测试: 嵌套循环");
58+
59+
// 多层嵌套循环
60+
nested_total : int = 0;
61+
for (i : 1..20) {
62+
for (j : 1..15) {
63+
for (k : 1..5) {
64+
nested_total = nested_total + i + j + k;
65+
};
66+
};
67+
};
68+
69+
print("嵌套循环基准测试结果: " + nested_total);
70+
return nested_total;
71+
};
72+
73+
fn benchmark_while_loop_performance(): int {
74+
print("📊 基准测试: While循环性能");
75+
76+
// While循环性能测试
77+
while_result : int = 0;
78+
counter : int = 1;
79+
while (counter <= 800) {
80+
while_result = while_result + counter;
81+
if (counter % 2 == 0) {
82+
while_result = while_result + counter / 2;
83+
};
84+
counter = counter + 1;
85+
};
86+
87+
print("While循环基准测试结果: " + while_result);
88+
return while_result;
89+
};
90+
91+
fn benchmark_optimization_effectiveness(): int {
92+
print("📊 基准测试: 优化效果");
93+
94+
optimization_sum : int = 0;
95+
96+
// 测试循环展开优化效果
97+
for (i : 1..100) {
98+
optimization_sum = optimization_sum + i;
99+
};
100+
101+
// 测试强度削减优化效果
102+
for (j : 1..80) {
103+
optimization_sum = optimization_sum + j * 16; // 乘以16,可优化为位移
104+
};
105+
106+
// 测试循环不变量提升效果
107+
constant_factor : int = 42;
108+
for (k : 1..60) {
109+
optimization_sum = optimization_sum + k + constant_factor;
110+
};
111+
112+
print("优化效果基准测试结果: " + optimization_sum);
113+
return optimization_sum;
114+
};
115+
116+
fn benchmark_cache_performance(): int {
117+
print("📊 基准测试: 缓存性能");
118+
119+
cache_total : int = 0;
120+
121+
// 重复执行相同模式,测试缓存命中率
122+
for (round : 1..10) {
123+
round_sum : int = 0;
124+
for (i : 1..50) {
125+
round_sum = round_sum + i * 4;
126+
};
127+
cache_total = cache_total + round_sum;
128+
};
129+
130+
print("缓存性能基准测试结果: " + cache_total);
131+
return cache_total;
132+
};
133+
134+
fn benchmark_mixed_workload(): int {
135+
print("📊 基准测试: 混合工作负载");
136+
137+
mixed_result : int = 0;
138+
139+
// 混合不同类型的循环
140+
// 1. 简单累加
141+
for (i : 1..100) {
142+
mixed_result = mixed_result + i;
143+
};
144+
145+
// 2. 条件处理
146+
for (j : 1..80) {
147+
if (j % 3 == 0) {
148+
mixed_result = mixed_result + j * 2;
149+
} else {
150+
mixed_result = mixed_result + j;
151+
};
152+
};
153+
154+
// 3. 嵌套计算
155+
for (k : 1..15) {
156+
inner_sum : int = 0;
157+
for (l : 1..10) {
158+
inner_sum = inner_sum + l;
159+
};
160+
mixed_result = mixed_result + inner_sum;
161+
};
162+
163+
print("混合工作负载基准测试结果: " + mixed_result);
164+
return mixed_result;
165+
};
166+
167+
fn benchmark_stress_test(): int {
168+
print("📊 基准测试: 压力测试");
169+
170+
stress_total : int = 0;
171+
172+
// 高强度循环测试
173+
for (batch : 1..5) {
174+
batch_sum : int = 0;
175+
176+
// 大量迭代
177+
for (i : 1..200) {
178+
temp_value : int = i;
179+
180+
// 多层计算
181+
for (j : 1..3) {
182+
temp_value = temp_value + j * i;
183+
};
184+
185+
batch_sum = batch_sum + temp_value;
186+
};
187+
188+
stress_total = stress_total + batch_sum;
189+
print("压力测试第" + batch + "批次: " + batch_sum);
190+
};
191+
192+
return stress_total;
193+
};
194+
195+
fn benchmark_compilation_overhead(): int {
196+
print("📊 基准测试: 编译开销");
197+
198+
compilation_result : int = 0;
199+
200+
// 测试不同复杂度的循环编译开销
201+
// 简单循环
202+
for (i : 1..50) {
203+
compilation_result = compilation_result + i;
204+
};
205+
206+
// 中等复杂度循环
207+
for (j : 1..40) {
208+
if (j % 2 == 0) {
209+
compilation_result = compilation_result + j * 3;
210+
} else {
211+
compilation_result = compilation_result + j;
212+
};
213+
};
214+
215+
// 复杂循环
216+
for (k : 1..30) {
217+
complex_value : int = k;
218+
if (k % 3 == 0) {
219+
complex_value = complex_value * 4;
220+
} else {
221+
if (k % 2 == 0) {
222+
complex_value = complex_value * 2;
223+
};
224+
};
225+
compilation_result = compilation_result + complex_value;
226+
};
227+
228+
print("编译开销基准测试结果: " + compilation_result);
229+
return compilation_result;
230+
};
231+
232+
fn main(): int {
233+
print("=== CodeNothing v0.7.7 JIT编译器基准测试套件 ===");
234+
print("🚀 开始执行基准测试...");
235+
print("");
236+
237+
// 执行各项基准测试
238+
result1 : int = benchmark_simple_loop();
239+
result2 : int = benchmark_arithmetic_intensive();
240+
result3 : int = benchmark_conditional_branches();
241+
result4 : int = benchmark_nested_loops();
242+
result5 : int = benchmark_while_loop_performance();
243+
print("");
244+
245+
result6 : int = benchmark_optimization_effectiveness();
246+
result7 : int = benchmark_cache_performance();
247+
result8 : int = benchmark_mixed_workload();
248+
print("");
249+
250+
result9 : int = benchmark_stress_test();
251+
result10 : int = benchmark_compilation_overhead();
252+
print("");
253+
254+
total : int = result1 + result2 + result3 + result4 + result5 +
255+
result6 + result7 + result8 + result9 + result10;
256+
257+
print("=== 基准测试完成 ===");
258+
print("总计基准测试结果: " + total);
259+
print("🎯 JIT编译器基准测试执行完毕!");
260+
print("请查看JIT调试输出以分析性能数据。");
261+
262+
return total;
263+
};

0 commit comments

Comments
 (0)