Helper to run benchmark on a JSON string
| 84 | |
| 85 | // Helper to run benchmark on a JSON string |
| 86 | void run_benchmark(const char* test_name, const fl::string& json_data, int iterations, bool& success) { |
| 87 | printf("\n"); |
| 88 | printf("================================================================================\n"); |
| 89 | printf("%s\n", test_name); |
| 90 | printf("================================================================================\n"); |
| 91 | printf("JSON size: %zu bytes (%.2f KB)\n", json_data.size(), json_data.size() / 1024.0); |
| 92 | printf("Iterations: %d\n", iterations); |
| 93 | printf("\n"); |
| 94 | |
| 95 | // Benchmark Legacy parse() |
| 96 | double parse1_time = benchmark_microseconds([&]() { |
| 97 | json result = json::parse(json_data); |
| 98 | if (result.is_null()) { |
| 99 | success = false; |
| 100 | } |
| 101 | // Force compiler to not optimize away |
| 102 | volatile bool valid = result.is_object() || result.is_array(); |
| 103 | (void)valid; |
| 104 | }, iterations); |
| 105 | |
| 106 | // Benchmark Custom parse2() |
| 107 | double parse2_time = benchmark_microseconds([&]() { |
| 108 | json result = json::parse(json_data); |
| 109 | if (result.is_null()) { |
| 110 | success = false; |
| 111 | } |
| 112 | // Force compiler to not optimize away |
| 113 | volatile bool valid = result.is_object() || result.is_array(); |
| 114 | (void)valid; |
| 115 | }, iterations); |
| 116 | |
| 117 | // Results |
| 118 | printf("Performance Results:\n"); |
| 119 | printf(" Legacy parse(): %.2f µs/parse\n", parse1_time); |
| 120 | printf(" Custom parse2(): %.2f µs/parse\n", parse2_time); |
| 121 | printf("\n"); |
| 122 | |
| 123 | // Comparison |
| 124 | printf("================================================================================\n"); |
| 125 | printf("COMPARISON\n"); |
| 126 | printf("================================================================================\n"); |
| 127 | |
| 128 | double speedup = parse1_time / parse2_time; |
| 129 | double ratio = parse2_time / parse1_time; |
| 130 | |
| 131 | if (parse2_time < parse1_time) { |
| 132 | printf("✓ parse2() is FASTER: %.2fx speedup (%.1f%% of parse() time)\n", |
| 133 | speedup, ratio * 100.0); |
| 134 | printf(" Time saved: %.2f µs per parse (%.1f%% reduction)\n", |
| 135 | parse1_time - parse2_time, (1.0 - ratio) * 100.0); |
| 136 | } else { |
| 137 | printf("✗ parse2() is SLOWER: %.2fx slowdown (%.1f%% of parse() time)\n", |
| 138 | 1.0 / speedup, ratio * 100.0); |
| 139 | printf(" Extra time: %.2f µs per parse (%.1f%% increase)\n", |
| 140 | parse2_time - parse1_time, (ratio - 1.0) * 100.0); |
| 141 | } |
| 142 | |
| 143 | // Throughput |