1use zigzag_rs::ZigZag;
2
3fn 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 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; 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 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 let mut values = [0i32; BATCH_SIZE];
34 let mut encoded = [0u32; BATCH_SIZE];
35
36 for i in 0..BATCH_SIZE {
38 values[i] = (i as i32) - (BATCH_SIZE as i32 / 2);
39 }
40
41 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 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 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}