benchmark/
benchmark.rs

1use zigzag_rs::ZigZag;
2
3// Since we don't depend on std, we use a simple method to measure performance
4fn main() {
5    const ITERATIONS: usize = 10_000_000;
6    const BATCH_SIZE: usize = 1000;
7    
8    println!("Running ZigZag encoding/decoding performance test");
9    
10    // Test single value i32 encoding
11    let start = std::time::Instant::now();
12    let mut sum = 0u32;
13    for i in 0..ITERATIONS {
14        let val = (i % 1000) as i32 - 500; // Cycle between -500 and 499
15        sum = sum.wrapping_add(i32::zigzag_encode(val));
16    }
17    let duration = start.elapsed();
18    println!("i32 single value encoding: {:?} for {} operations (checksum: {})", 
19             duration, ITERATIONS, sum);
20    
21    // Test single value i32 decoding
22    let start = std::time::Instant::now();
23    let mut sum = 0i32;
24    for i in 0..ITERATIONS {
25        let val = (i % 1000) as u32;
26        sum = sum.wrapping_add(i32::zigzag_decode(val));
27    }
28    let duration = start.elapsed();
29    println!("i32 single value decoding: {:?} for {} operations (checksum: {})", 
30             duration, ITERATIONS, sum);
31    
32    // Test batch i32 encoding
33    let mut values = [0i32; BATCH_SIZE];
34    let mut encoded = [0u32; BATCH_SIZE];
35    
36    // Initialize test data
37    for i in 0..BATCH_SIZE {
38        values[i] = (i as i32) - (BATCH_SIZE as i32 / 2);
39    }
40    
41    // Measure batch encoding performance
42    let batch_iterations = ITERATIONS / BATCH_SIZE;
43    let start = std::time::Instant::now();
44    for _ in 0..batch_iterations {
45        i32::zigzag_encode_slice(&values, &mut encoded);
46    }
47    let duration = start.elapsed();
48    println!("i32 batch encoding: {:?} for {} batches of {} values (total: {} operations)", 
49             duration, batch_iterations, BATCH_SIZE, batch_iterations * BATCH_SIZE);
50    
51    // Measure batch decoding performance
52    let start = std::time::Instant::now();
53    for _ in 0..batch_iterations {
54        i32::zigzag_decode_slice(&encoded, &mut values);
55    }
56    let duration = start.elapsed();
57    println!("i32 batch decoding: {:?} for {} batches of {} values (total: {} operations)", 
58             duration, batch_iterations, BATCH_SIZE, batch_iterations * BATCH_SIZE);
59    
60    // Check if expected values after round-trip are preserved
61    let mut original = [0i32; BATCH_SIZE];
62    for i in 0..BATCH_SIZE {
63        original[i] = (i as i32) - (BATCH_SIZE as i32 / 2);
64    }
65    assert_eq!(values, original, "Batch round-trip values should be preserved");
66    
67    println!("\nComparing single value vs batch processing efficiency:");
68    println!("This comparison shows if batch processing provides any optimization beyond \
69              just calling single operations in sequence.");
70}