mathcompile 0.1.2

High-performance symbolic mathematics with final tagless design, egglog optimization, and Rust hot-loading compilation
Documentation
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Real Performance Comparison: `MathCompile` Symbolic AD vs `ad_trait`
//!
//! This benchmark provides ACTUAL measured performance comparisons between
//! our symbolic automatic differentiation and the `ad_trait` library.
//!
//! **NEW**: Now uses Rust hot-loading compilation for maximum performance!

#[cfg(feature = "ad_trait")]
use ad_trait::AD;
#[cfg(feature = "ad_trait")]
use ad_trait::differentiable_function::{DifferentiableFunctionTrait, ForwardAD, ForwardADMulti};
#[cfg(feature = "ad_trait")]
use ad_trait::forward_ad::adfn::adfn;
#[cfg(feature = "ad_trait")]
use ad_trait::function_engine::FunctionEngine;

use libloading::{Library, Symbol};
use mathcompile::backends::rust_codegen::RustOptLevel;
use mathcompile::backends::{RustCodeGenerator, RustCompiler};
use mathcompile::final_tagless::{ASTEval, ASTMathExpr};
use mathcompile::symbolic_ad::convenience;
use std::fs;
use std::time::Instant;

/// Real benchmark results with actual measurements
#[derive(Debug, Clone)]
struct BenchmarkResults {
    /// Actual measured time for symbolic AD (microseconds)
    symbolic_ad_time_us: u64,
    /// Actual measured time for `ad_trait` (microseconds)
    ad_trait_time_us: u64,
    /// Accuracy comparison
    accuracy_difference: f64,
    /// Test description
    test_name: String,
    /// Compilation time for Rust codegen (microseconds)
    compilation_time_us: u64,
}

/// Compiled function wrapper for Rust hot-loading
struct CompiledFunction {
    _library: Library,
    function: Symbol<'static, extern "C" fn(f64, f64) -> f64>,
}

impl CompiledFunction {
    /// Load a compiled function from a dynamic library
    unsafe fn load(
        lib_path: &std::path::Path,
        func_name: &str,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        unsafe {
            let library = Library::new(lib_path)?;
            let function: Symbol<extern "C" fn(f64, f64) -> f64> =
                library.get(format!("{func_name}_two_vars").as_bytes())?;
            let function = std::mem::transmute(function);

            Ok(Self {
                _library: library,
                function,
            })
        }
    }

    /// Call the compiled function
    fn call(&self, x: f64, y: f64) -> f64 {
        (self.function)(x, y)
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🔬 REAL Performance Comparison: MathCompile Symbolic AD (Rust Codegen) vs ad_trait");
    println!("=============================================================================\n");

    #[cfg(not(feature = "ad_trait"))]
    {
        println!("❌ This benchmark requires the 'ad_trait' feature to be enabled.");
        println!("   Run with: cargo run --example real_ad_performance --features ad_trait");
        return Ok(());
    }

    // Check if rustc is available
    if !RustCompiler::is_available() {
        println!(
            "❌ rustc is not available. Rust codegen benchmarks require rustc to be installed."
        );
        return Ok(());
    }

    println!("✅ Using Rust hot-loading compilation for symbolic AD");
    println!("   Rustc version: {}", RustCompiler::version_info()?);
    println!();

    #[cfg(feature = "ad_trait")]
    {
        // Setup temporary directories for Rust compilation
        let temp_dir = std::env::temp_dir().join("mathcompile_ad_bench");
        let source_dir = temp_dir.join("sources");
        let lib_dir = temp_dir.join("libs");

        // Clean and create directories
        let _ = fs::remove_dir_all(&temp_dir);
        fs::create_dir_all(&source_dir)?;
        fs::create_dir_all(&lib_dir)?;

        // 1. Simple Quadratic Function
        println!("1️⃣  Simple Quadratic: f(x) = x² (Rust Codegen)");
        let result1 = benchmark_simple_quadratic_rust(1000, &source_dir, &lib_dir)?;
        print_results(&result1);
        println!();

        // 2. Polynomial Function
        println!("2️⃣  Polynomial: f(x) = x⁴ + 3x³ + 2x² + x + 1 (Rust Codegen)");
        let result2 = benchmark_polynomial_rust(500, &source_dir, &lib_dir)?;
        print_results(&result2);
        println!();

        // 3. Multivariate Function
        println!("3️⃣  Multivariate: f(x,y) = x² + 2xy + y² (Rust Codegen)");
        let result3 = benchmark_multivariate_rust(500, &source_dir, &lib_dir)?;
        print_results(&result3);
        println!();

        // Summary
        print_summary(&[result1, result2, result3]);

        // Cleanup
        let _ = fs::remove_dir_all(&temp_dir);
    }

    Ok(())
}

#[cfg(feature = "ad_trait")]
#[derive(Clone)]
struct SimpleQuadratic<T: AD> {
    _phantom: std::marker::PhantomData<T>,
}

#[cfg(feature = "ad_trait")]
impl<T: AD> DifferentiableFunctionTrait<T> for SimpleQuadratic<T> {
    const NAME: &'static str = "SimpleQuadratic";

    fn call(&self, inputs: &[T], _freeze: bool) -> Vec<T> {
        vec![inputs[0] * inputs[0]]
    }

    fn num_inputs(&self) -> usize {
        1
    }

    fn num_outputs(&self) -> usize {
        1
    }
}

#[cfg(feature = "ad_trait")]
impl<T: AD> SimpleQuadratic<T> {
    fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    fn to_other_ad_type<T2: AD>(&self) -> SimpleQuadratic<T2> {
        SimpleQuadratic::new()
    }
}

#[cfg(feature = "ad_trait")]
fn benchmark_simple_quadratic_rust(
    iterations: usize,
    source_dir: &std::path::Path,
    lib_dir: &std::path::Path,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    // Symbolic AD version - PRE-COMPILE the derivative with enhanced optimization
    let expr = ASTEval::pow(ASTEval::var_by_name("x"), ASTEval::constant(2.0));

    // Enable enhanced optimization
    let mut config = mathcompile::symbolic_ad::SymbolicADConfig::default();
    config.pre_optimize = true;
    config.post_optimize = true;
    config.num_variables = 1; // x

    let mut symbolic_ad = mathcompile::symbolic_ad::SymbolicAD::with_config(config)?;
    let result = symbolic_ad.compute_with_derivatives(&expr)?;
    let symbolic_grad = &result.first_derivatives["x"];

    println!("  📊 Optimization stats:");
    println!(
        "    Function operations before: {}",
        result.stats.function_operations_before
    );
    println!(
        "    Function operations after: {}",
        result.stats.function_operations_after
    );
    println!(
        "    Total operations before: {}",
        result.stats.total_operations_before
    );
    println!(
        "    Total operations after: {}",
        result.stats.total_operations_after
    );

    if result.stats.function_operations_before > result.stats.function_operations_after {
        let reduction = 100.0 * (1.0 - result.stats.function_optimization_ratio());
        println!("    🎯 Function optimized by {reduction:.1}%");
    } else if result.stats.function_operations_after > result.stats.function_operations_before {
        let increase = 100.0 * (result.stats.function_optimization_ratio() - 1.0);
        println!(
            "    📈 Function complexity increased by {increase:.1}% (due to optimization rules)"
        );
    }

    if result.stats.total_operations_before > result.stats.total_operations_after {
        let reduction = 100.0 * (1.0 - result.stats.total_optimization_ratio());
        println!("    🎯 Total pipeline optimized by {reduction:.1}%");
    }

    // Compile the derivative to Rust code
    let codegen = RustCodeGenerator::new();
    let compiler = RustCompiler::with_opt_level(RustOptLevel::O2);

    let func_name = "simple_quadratic_grad";
    let rust_source = codegen.generate_function(symbolic_grad, func_name)?;

    let source_path = source_dir.join(format!("{func_name}.rs"));
    let lib_path = lib_dir.join(format!("lib{func_name}.so"));

    // Time the compilation
    let compile_start = Instant::now();
    compiler.compile_dylib(&rust_source, &source_path, &lib_path)?;
    let compilation_time = compile_start.elapsed().as_micros() as u64;

    println!("  🔧 Rust compilation time: {compilation_time} μs");

    // Load the compiled function
    let compiled_func = unsafe { CompiledFunction::load(&lib_path, func_name)? };

    // Now time just the EXECUTION
    let start = Instant::now();
    for _ in 0..iterations {
        let _result = compiled_func.call(2.0, 0.0);
    }
    let symbolic_time = start.elapsed().as_micros() as u64;

    // ad_trait version - PRE-COMPILE the function engine
    let function_standard = SimpleQuadratic::<f64>::new();
    let function_derivative = function_standard.to_other_ad_type::<adfn<1>>();
    let differentiable_block =
        FunctionEngine::new(function_standard, function_derivative, ForwardAD::new());
    let inputs = vec![2.0];

    // Now time just the EXECUTION
    let start = Instant::now();
    for _ in 0..iterations {
        let _result = differentiable_block.derivative(&inputs);
    }
    let ad_trait_time = start.elapsed().as_micros() as u64;

    // Accuracy check
    let symbolic_result = compiled_func.call(2.0, 0.0);
    let (_, ad_trait_grad) = differentiable_block.derivative(&inputs);
    let ad_trait_result = ad_trait_grad[(0, 0)];

    Ok(BenchmarkResults {
        symbolic_ad_time_us: symbolic_time,
        ad_trait_time_us: ad_trait_time,
        accuracy_difference: (symbolic_result - ad_trait_result).abs(),
        test_name: "Simple Quadratic".to_string(),
        compilation_time_us: compilation_time,
    })
}

#[cfg(feature = "ad_trait")]
#[derive(Clone)]
struct Polynomial<T: AD> {
    _phantom: std::marker::PhantomData<T>,
}

#[cfg(feature = "ad_trait")]
impl<T: AD> DifferentiableFunctionTrait<T> for Polynomial<T> {
    const NAME: &'static str = "Polynomial";

    fn call(&self, inputs: &[T], _freeze: bool) -> Vec<T> {
        let x = inputs[0];
        // x⁴ + 3x³ + 2x² + x + 1
        let x2 = x * x;
        let x3 = x2 * x;
        let x4 = x3 * x;
        let three = T::from_f64(3.0).unwrap_or_else(|| panic!("Failed to convert 3.0"));
        let two = T::from_f64(2.0).unwrap_or_else(|| panic!("Failed to convert 2.0"));
        let one = T::from_f64(1.0).unwrap_or_else(|| panic!("Failed to convert 1.0"));
        vec![x4 + x3 * three + x2 * two + x + one]
    }

    fn num_inputs(&self) -> usize {
        1
    }

    fn num_outputs(&self) -> usize {
        1
    }
}

#[cfg(feature = "ad_trait")]
impl<T: AD> Polynomial<T> {
    fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    fn to_other_ad_type<T2: AD>(&self) -> Polynomial<T2> {
        Polynomial::new()
    }
}

#[cfg(feature = "ad_trait")]
fn benchmark_polynomial_rust(
    iterations: usize,
    source_dir: &std::path::Path,
    lib_dir: &std::path::Path,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    // Symbolic AD: f(x) = x⁴ + 3x³ + 2x² + x + 1 with enhanced optimization
    let expr = ASTEval::add(
        ASTEval::add(
            ASTEval::add(
                ASTEval::add(
                    ASTEval::pow(ASTEval::var_by_name("x"), ASTEval::constant(4.0)),
                    ASTEval::mul(
                        ASTEval::constant(3.0),
                        ASTEval::pow(ASTEval::var_by_name("x"), ASTEval::constant(3.0)),
                    ),
                ),
                ASTEval::mul(
                    ASTEval::constant(2.0),
                    ASTEval::pow(ASTEval::var_by_name("x"), ASTEval::constant(2.0)),
                ),
            ),
            ASTEval::var_by_name("x"),
        ),
        ASTEval::constant(1.0),
    );

    // Enable enhanced optimization
    let mut config = mathcompile::symbolic_ad::SymbolicADConfig::default();
    config.pre_optimize = true;
    config.post_optimize = true;
    config.num_variables = 1; // x

    let mut symbolic_ad = mathcompile::symbolic_ad::SymbolicAD::with_config(config)?;
    let result = symbolic_ad.compute_with_derivatives(&expr)?;
    let symbolic_grad = &result.first_derivatives["x"];

    println!("  📊 Optimization stats:");
    println!(
        "    Function operations before: {}",
        result.stats.function_operations_before
    );
    println!(
        "    Function operations after: {}",
        result.stats.function_operations_after
    );
    println!(
        "    Total operations before: {}",
        result.stats.total_operations_before
    );
    println!(
        "    Total operations after: {}",
        result.stats.total_operations_after
    );

    if result.stats.function_operations_before > result.stats.function_operations_after {
        let reduction = 100.0 * (1.0 - result.stats.function_optimization_ratio());
        println!("    🎯 Function optimized by {reduction:.1}%");
    } else if result.stats.function_operations_after > result.stats.function_operations_before {
        let increase = 100.0 * (result.stats.function_optimization_ratio() - 1.0);
        println!(
            "    📈 Function complexity increased by {increase:.1}% (due to optimization rules)"
        );
    }

    if result.stats.total_operations_before > result.stats.total_operations_after {
        let reduction = 100.0 * (1.0 - result.stats.total_optimization_ratio());
        println!("    🎯 Total pipeline optimized by {reduction:.1}%");
    }

    // Compile the derivative to Rust code
    let codegen = RustCodeGenerator::new();
    let compiler = RustCompiler::with_opt_level(RustOptLevel::O2);

    let func_name = "polynomial_grad";
    let rust_source = codegen.generate_function(symbolic_grad, func_name)?;

    let source_path = source_dir.join(format!("{func_name}.rs"));
    let lib_path = lib_dir.join(format!("lib{func_name}.so"));

    // Time the compilation
    let compile_start = Instant::now();
    compiler.compile_dylib(&rust_source, &source_path, &lib_path)?;
    let compilation_time = compile_start.elapsed().as_micros() as u64;

    println!("  🔧 Rust compilation time: {compilation_time} μs");

    // Load the compiled function
    let compiled_func = unsafe { CompiledFunction::load(&lib_path, func_name)? };

    // Now time just the EXECUTION
    let start = Instant::now();
    for _ in 0..iterations {
        let _result = compiled_func.call(2.0, 0.0);
    }
    let symbolic_time = start.elapsed().as_micros() as u64;

    // ad_trait version - PRE-COMPILE
    let function_standard = Polynomial::<f64>::new();
    let function_derivative = function_standard.to_other_ad_type::<adfn<1>>();
    let differentiable_block =
        FunctionEngine::new(function_standard, function_derivative, ForwardAD::new());
    let inputs = vec![2.0];

    // Now time just the EXECUTION
    let start = Instant::now();
    for _ in 0..iterations {
        let _result = differentiable_block.derivative(&inputs);
    }
    let ad_trait_time = start.elapsed().as_micros() as u64;

    // Accuracy check
    let symbolic_result = compiled_func.call(2.0, 0.0);
    let (_, ad_trait_grad) = differentiable_block.derivative(&inputs);
    let ad_trait_result = ad_trait_grad[(0, 0)];

    Ok(BenchmarkResults {
        symbolic_ad_time_us: symbolic_time,
        ad_trait_time_us: ad_trait_time,
        accuracy_difference: (symbolic_result - ad_trait_result).abs(),
        test_name: "Polynomial".to_string(),
        compilation_time_us: compilation_time,
    })
}

#[cfg(feature = "ad_trait")]
#[derive(Clone)]
struct Multivariate<T: AD> {
    _phantom: std::marker::PhantomData<T>,
}

#[cfg(feature = "ad_trait")]
impl<T: AD> DifferentiableFunctionTrait<T> for Multivariate<T> {
    const NAME: &'static str = "Multivariate";

    fn call(&self, inputs: &[T], _freeze: bool) -> Vec<T> {
        let x = inputs[0];
        let y = inputs[1];
        let two = T::from_f64(2.0).unwrap_or_else(|| panic!("Failed to convert 2.0"));
        // x² + 2xy + y²
        vec![x * x + two * x * y + y * y]
    }

    fn num_inputs(&self) -> usize {
        2
    }

    fn num_outputs(&self) -> usize {
        1
    }
}

#[cfg(feature = "ad_trait")]
impl<T: AD> Multivariate<T> {
    fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
        }
    }

    fn to_other_ad_type<T2: AD>(&self) -> Multivariate<T2> {
        Multivariate::new()
    }
}

#[cfg(feature = "ad_trait")]
fn benchmark_multivariate_rust(
    iterations: usize,
    source_dir: &std::path::Path,
    lib_dir: &std::path::Path,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    // Symbolic AD: f(x,y) = x² + 2xy + y²
    let expr = ASTEval::add(
        ASTEval::add(
            ASTEval::pow(ASTEval::var_by_name("x"), ASTEval::constant(2.0)),
            ASTEval::mul(
                ASTEval::constant(2.0),
                ASTEval::mul(ASTEval::var_by_name("x"), ASTEval::var_by_name("y")),
            ),
        ),
        ASTEval::pow(ASTEval::var_by_name("y"), ASTEval::constant(2.0)),
    );
    let symbolic_grad = convenience::gradient(&expr, &["x", "y"])?; // Pre-compile

    // Compile both partial derivatives to Rust code
    let codegen = RustCodeGenerator::new();
    let compiler = RustCompiler::with_opt_level(RustOptLevel::O2);

    // Compile dx
    let func_name_dx = "multivariate_grad_dx";
    let rust_source_dx = codegen.generate_function(&symbolic_grad["x"], func_name_dx)?;
    let source_path_dx = source_dir.join(format!("{func_name_dx}.rs"));
    let lib_path_dx = lib_dir.join(format!("lib{func_name_dx}.so"));

    // Compile dy
    let func_name_dy = "multivariate_grad_dy";
    let rust_source_dy = codegen.generate_function(&symbolic_grad["y"], func_name_dy)?;
    let source_path_dy = source_dir.join(format!("{func_name_dy}.rs"));
    let lib_path_dy = lib_dir.join(format!("lib{func_name_dy}.so"));

    // Time the compilation
    let compile_start = Instant::now();
    compiler.compile_dylib(&rust_source_dx, &source_path_dx, &lib_path_dx)?;
    compiler.compile_dylib(&rust_source_dy, &source_path_dy, &lib_path_dy)?;
    let compilation_time = compile_start.elapsed().as_micros() as u64;

    println!("  🔧 Rust compilation time: {compilation_time} μs");

    // Load the compiled functions
    let compiled_func_dx = unsafe { CompiledFunction::load(&lib_path_dx, func_name_dx)? };
    let compiled_func_dy = unsafe { CompiledFunction::load(&lib_path_dy, func_name_dy)? };

    // Now time just the EXECUTION
    let start = Instant::now();
    for _ in 0..iterations {
        let symbolic_dx = compiled_func_dx.call(1.0, 2.0);
        let symbolic_dy = compiled_func_dy.call(1.0, 2.0);
        let _result = (symbolic_dx, symbolic_dy);
    }
    let symbolic_time = start.elapsed().as_micros() as u64;

    // ad_trait version - PRE-COMPILE
    let function_standard = Multivariate::<f64>::new();
    let function_derivative = function_standard.to_other_ad_type::<adfn<2>>();
    let differentiable_block = FunctionEngine::new(
        function_standard,
        function_derivative,
        ForwardADMulti::new(),
    );
    let inputs = vec![1.0, 2.0];

    // Now time just the EXECUTION
    let start = Instant::now();
    for _ in 0..iterations {
        let _result = differentiable_block.derivative(&inputs);
    }
    let ad_trait_time = start.elapsed().as_micros() as u64;

    // Accuracy check
    let symbolic_dx = compiled_func_dx.call(1.0, 2.0);
    let symbolic_dy = compiled_func_dy.call(1.0, 2.0);

    let (_, ad_trait_grad) = differentiable_block.derivative(&inputs);
    let ad_trait_dx = ad_trait_grad[(0, 0)];
    let ad_trait_dy = ad_trait_grad[(0, 1)];

    let accuracy_diff = (symbolic_dx - ad_trait_dx).abs() + (symbolic_dy - ad_trait_dy).abs();

    Ok(BenchmarkResults {
        symbolic_ad_time_us: symbolic_time,
        ad_trait_time_us: ad_trait_time,
        accuracy_difference: accuracy_diff,
        test_name: "Multivariate".to_string(),
        compilation_time_us: compilation_time,
    })
}

fn print_results(results: &BenchmarkResults) {
    let speedup = if results.symbolic_ad_time_us < results.ad_trait_time_us {
        results.ad_trait_time_us as f64 / results.symbolic_ad_time_us as f64
    } else {
        -(results.symbolic_ad_time_us as f64 / results.ad_trait_time_us as f64)
    };

    println!("  📊 Results:");
    println!("    Symbolic AD:  {} μs", results.symbolic_ad_time_us);
    println!("    ad_trait:     {} μs", results.ad_trait_time_us);
    println!("    Compilation:  {} μs", results.compilation_time_us);

    if speedup > 0.0 {
        println!("    🚀 Symbolic AD is {speedup:.1}x faster");
    } else {
        println!("    📈 ad_trait is {:.1}x faster", -speedup);
    }

    println!("    Accuracy diff: {:.2e}", results.accuracy_difference);
}

fn print_summary(results: &[BenchmarkResults]) {
    println!("📋 **RUST CODEGEN BENCHMARK SUMMARY**");
    println!("====================================\n");

    let mut symbolic_wins = 0;
    let mut ad_trait_wins = 0;
    let mut total_symbolic_time = 0;
    let mut total_ad_trait_time = 0;
    let mut total_compilation_time = 0;

    for result in results {
        total_symbolic_time += result.symbolic_ad_time_us;
        total_ad_trait_time += result.ad_trait_time_us;
        total_compilation_time += result.compilation_time_us;

        if result.symbolic_ad_time_us < result.ad_trait_time_us {
            symbolic_wins += 1;
        } else {
            ad_trait_wins += 1;
        }
    }

    println!("🏆 **Performance Summary**:");
    println!("  Symbolic AD wins: {symbolic_wins} tests");
    println!("  ad_trait wins:    {ad_trait_wins} tests");

    if total_symbolic_time > 0 {
        println!(
            "  Total execution time ratio: {:.2}x",
            total_ad_trait_time as f64 / total_symbolic_time as f64
        );
    }

    println!("  Total compilation time: {total_compilation_time} μs");
    println!(
        "  Compilation overhead: {:.1}% of execution time",
        100.0 * total_compilation_time as f64 / total_symbolic_time as f64
    );

    println!();

    println!("🎯 **Key Findings**:");
    println!("  • Rust codegen provides native machine code performance");
    println!("  • Compilation overhead is amortized over repeated evaluations");
    println!("  • Symbolic optimization reduces expression complexity before compilation");
    println!("  • Hot-loading enables maximum performance for production workloads");
    println!();

    println!("💡 **Use Case Recommendations**:");
    println!(
        "  • Use Rust codegen for: production systems, repeated evaluation, maximum performance"
    );
    println!("  • Use ad_trait for: prototyping, one-off computations, immediate results");
    println!("  • Consider compilation cost vs. evaluation frequency trade-offs");
    println!("  • Symbolic optimization is crucial for complex expressions");
}

// Stub implementations for when ad_trait is not available
#[cfg(not(feature = "ad_trait"))]
fn benchmark_simple_quadratic_rust(
    _iterations: usize,
    _source_dir: &std::path::Path,
    _lib_dir: &std::path::Path,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    Ok(BenchmarkResults {
        symbolic_ad_time_us: 0,
        ad_trait_time_us: 0,
        accuracy_difference: 0.0,
        test_name: "Stub".to_string(),
        compilation_time_us: 0,
    })
}

#[cfg(not(feature = "ad_trait"))]
fn benchmark_polynomial_rust(
    _iterations: usize,
    _source_dir: &std::path::Path,
    _lib_dir: &std::path::Path,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    Ok(BenchmarkResults {
        symbolic_ad_time_us: 0,
        ad_trait_time_us: 0,
        accuracy_difference: 0.0,
        test_name: "Stub".to_string(),
        compilation_time_us: 0,
    })
}

#[cfg(not(feature = "ad_trait"))]
fn benchmark_multivariate_rust(
    _iterations: usize,
    _source_dir: &std::path::Path,
    _lib_dir: &std::path::Path,
) -> Result<BenchmarkResults, Box<dyn std::error::Error>> {
    Ok(BenchmarkResults {
        symbolic_ad_time_us: 0,
        ad_trait_time_us: 0,
        accuracy_difference: 0.0,
        test_name: "Stub".to_string(),
        compilation_time_us: 0,
    })
}