|
| 1 | +using lib <io>; |
| 2 | + |
| 3 | +// CodeNothing v0.7.5 内存池性能基准测试 |
| 4 | +// 测试内存预分配池的性能提升效果 |
| 5 | + |
| 6 | +fn test_memory_intensive_operations(): int { |
| 7 | + println("=== 测试内存密集型操作 ==="); |
| 8 | + |
| 9 | + // 测试大量变量分配 |
| 10 | + i : int = 0; |
| 11 | + total : int = 0; |
| 12 | + while (i < 1000) { |
| 13 | + // 创建临时变量进行计算 |
| 14 | + temp1 : int = i * 2; |
| 15 | + temp2 : int = temp1 + 5; |
| 16 | + temp3 : int = temp2 * 3; |
| 17 | + temp4 : int = temp3 - 1; |
| 18 | + temp5 : int = temp4 / 2; |
| 19 | + |
| 20 | + // 累加结果 |
| 21 | + total = total + temp5; |
| 22 | + i = i + 1; |
| 23 | + }; |
| 24 | + |
| 25 | + println("大量变量分配测试完成"); |
| 26 | + return total; |
| 27 | +}; |
| 28 | + |
| 29 | +fn test_array_operations(): int { |
| 30 | + println("=== 测试数组操作 ==="); |
| 31 | + |
| 32 | + // 模拟数组操作(通过多个变量) |
| 33 | + arr0 : int = 0; |
| 34 | + arr1 : int = 1; |
| 35 | + arr2 : int = 2; |
| 36 | + arr3 : int = 3; |
| 37 | + arr4 : int = 4; |
| 38 | + arr5 : int = 5; |
| 39 | + arr6 : int = 6; |
| 40 | + arr7 : int = 7; |
| 41 | + arr8 : int = 8; |
| 42 | + arr9 : int = 9; |
| 43 | + |
| 44 | + // 对数组进行操作 |
| 45 | + sum : int = arr0 + arr1 + arr2 + arr3 + arr4; |
| 46 | + sum = sum + arr5 + arr6 + arr7 + arr8 + arr9; |
| 47 | + |
| 48 | + // 重复操作以增加内存压力 |
| 49 | + i : int = 0; |
| 50 | + while (i < 100) { |
| 51 | + temp_sum : int = sum * 2; |
| 52 | + temp_avg : int = temp_sum / 10; |
| 53 | + temp_result : int = temp_avg + i; |
| 54 | + sum = temp_result; |
| 55 | + i = i + 1; |
| 56 | + }; |
| 57 | + |
| 58 | + println("数组操作测试完成"); |
| 59 | + return sum; |
| 60 | +}; |
| 61 | + |
| 62 | +fn test_nested_calculations(): int { |
| 63 | + println("=== 测试嵌套计算 ==="); |
| 64 | + |
| 65 | + result : int = 0; |
| 66 | + outer : int = 0; |
| 67 | + |
| 68 | + while (outer < 50) { |
| 69 | + inner : int = 0; |
| 70 | + local_sum : int = 0; |
| 71 | + |
| 72 | + while (inner < 20) { |
| 73 | + // 创建多层嵌套的临时变量 |
| 74 | + level1 : int = inner * outer; |
| 75 | + level2 : int = level1 + 10; |
| 76 | + level3 : int = level2 * 2; |
| 77 | + level4 : int = level3 - 5; |
| 78 | + level5 : int = level4 / 3; |
| 79 | + |
| 80 | + local_sum = local_sum + level5; |
| 81 | + inner = inner + 1; |
| 82 | + }; |
| 83 | + |
| 84 | + result = result + local_sum; |
| 85 | + outer = outer + 1; |
| 86 | + }; |
| 87 | + |
| 88 | + println("嵌套计算测试完成"); |
| 89 | + return result; |
| 90 | +}; |
| 91 | + |
| 92 | +fn test_string_like_operations(): int { |
| 93 | + println("=== 测试字符串类操作 ==="); |
| 94 | + |
| 95 | + // 模拟字符串操作(通过整数运算) |
| 96 | + char1 : int = 65; // 'A' |
| 97 | + char2 : int = 66; // 'B' |
| 98 | + char3 : int = 67; // 'C' |
| 99 | + char4 : int = 68; // 'D' |
| 100 | + char5 : int = 69; // 'E' |
| 101 | + |
| 102 | + // 模拟字符串拼接和处理 |
| 103 | + i : int = 0; |
| 104 | + result : int = 0; |
| 105 | + |
| 106 | + while (i < 200) { |
| 107 | + // 模拟字符串操作 |
| 108 | + concat1 : int = char1 + char2; |
| 109 | + concat2 : int = char3 + char4; |
| 110 | + concat3 : int = char5 + i; |
| 111 | + |
| 112 | + // 模拟字符串长度计算 |
| 113 | + length : int = concat1 + concat2 + concat3; |
| 114 | + |
| 115 | + // 模拟字符串比较 |
| 116 | + comparison : int = length - (char1 * 5); |
| 117 | + |
| 118 | + result = result + comparison; |
| 119 | + i = i + 1; |
| 120 | + }; |
| 121 | + |
| 122 | + println("字符串类操作测试完成"); |
| 123 | + return result; |
| 124 | +}; |
| 125 | + |
| 126 | +fn test_function_call_overhead(): int { |
| 127 | + println("=== 测试函数调用开销 ==="); |
| 128 | + |
| 129 | + total : int = 0; |
| 130 | + i : int = 0; |
| 131 | + |
| 132 | + while (i < 100) { |
| 133 | + // 多次调用函数增加内存分配压力 |
| 134 | + result1 : int = test_simple_calculation(i); |
| 135 | + result2 : int = test_simple_calculation(i + 1); |
| 136 | + result3 : int = test_simple_calculation(i + 2); |
| 137 | + |
| 138 | + total = total + result1 + result2 + result3; |
| 139 | + i = i + 1; |
| 140 | + }; |
| 141 | + |
| 142 | + println("函数调用开销测试完成"); |
| 143 | + return total; |
| 144 | +}; |
| 145 | + |
| 146 | +fn test_simple_calculation(x: int): int { |
| 147 | + // 简单计算函数,用于测试函数调用开销 |
| 148 | + temp1 : int = x * 2; |
| 149 | + temp2 : int = temp1 + 5; |
| 150 | + temp3 : int = temp2 - 1; |
| 151 | + return temp3; |
| 152 | +}; |
| 153 | + |
| 154 | +fn main(): int { |
| 155 | + println("=== CodeNothing v0.7.5 内存池性能基准测试 ==="); |
| 156 | + println("测试内存预分配池的性能提升效果"); |
| 157 | + println(""); |
| 158 | + |
| 159 | + // 执行各种内存密集型测试 |
| 160 | + result1 : int = test_memory_intensive_operations(); |
| 161 | + result2 : int = test_array_operations(); |
| 162 | + result3 : int = test_nested_calculations(); |
| 163 | + result4 : int = test_string_like_operations(); |
| 164 | + result5 : int = test_function_call_overhead(); |
| 165 | + |
| 166 | + // 计算总结果 |
| 167 | + total_result : int = result1 + result2 + result3 + result4 + result5; |
| 168 | + |
| 169 | + println(""); |
| 170 | + println("=== 测试结果汇总 ==="); |
| 171 | + println("内存密集型操作结果: " + result1); |
| 172 | + println("数组操作结果: " + result2); |
| 173 | + println("嵌套计算结果: " + result3); |
| 174 | + println("字符串类操作结果: " + result4); |
| 175 | + println("函数调用开销结果: " + result5); |
| 176 | + println("总计算结果: " + total_result); |
| 177 | + println(""); |
| 178 | + println("内存池性能基准测试完成!"); |
| 179 | + |
| 180 | + return 0; |
| 181 | +}; |
0 commit comments