pub struct SimpleBench { /* private fields */ }Implementations§
Source§impl SimpleBench
impl SimpleBench
Sourcepub fn new(name: &str) -> Self
pub fn new(name: &str) -> Self
Examples found in repository?
examples/simple_benchmark_example.rs (line 15)
5fn main() {
6 quick_calibrate_tsc_frequency();
7
8 #[cfg(target_arch = "aarch64")]
9 {
10 let resolution_ns = 1_000_000_000u64 / 24_000_000u64;
11 println!("Note: ARM64 timing resolution is ~{}ns. Very fast operations may show limited precision.\n", resolution_ns);
12 }
13
14 println!("=== Simple Arithmetic Benchmark ===");
15 SimpleBench::new("arithmetic")
16 .bench(10000, || {
17 let a = 42u64;
18 let b = 37u64;
19 a.wrapping_add(b).wrapping_mul(2)
20 })
21 .report();
22
23 println!("\n=== Vector Allocation Benchmark ===");
24 SimpleBench::new("vec_allocation")
25 .bench(1000, || {
26 let vec: Vec<u64> = Vec::with_capacity(100);
27 drop(vec);
28 })
29 .report();
30
31 println!("\n=== Function Call Overhead ===");
32 fn dummy_function(x: u64) -> u64 {
33 x.wrapping_add(1)
34 }
35
36 SimpleBench::new("function_call")
37 .bench(5000, || dummy_function(42))
38 .report();
39
40 println!("\n=== Custom Analysis Example ===");
41 let analysis = SimpleBench::new("analysis_example")
42 .bench(1000, || std::hint::black_box(42))
43 .analyze();
44
45 if analysis.meets_target(100) {
46 println!("✓ Performance target met! P99: {}ns", analysis.p99);
47 } else {
48 println!("✗ Performance target missed. P99: {}ns", analysis.p99);
49 }
50
51 println!("\n=== Implementation Comparison ===");
52
53 let old_analysis = SimpleBench::new("old_impl")
54 .bench(1000, || {
55 for _ in 0..10 {
56 std::hint::black_box(42);
57 }
58 })
59 .analyze();
60
61 let new_analysis = SimpleBench::new("new_impl")
62 .bench(1000, || {
63 std::hint::black_box(42);
64 })
65 .analyze();
66
67 println!("Old: {}ns P99, New: {}ns P99", old_analysis.p99, new_analysis.p99);
68
69 if new_analysis.mean == 0 {
70 println!("Improvement: Operation too fast to measure precisely (< {}ns resolution)",
71 1_000_000_000u64 / 24_000_000u64);
72 } else {
73 let improvement = (old_analysis.mean as f64 / new_analysis.mean as f64 - 1.0) * 100.0;
74 println!("Improvement: {:.1}% faster", improvement);
75 }
76}More examples
examples/custom_benchmark.rs (line 33)
26fn main() {
27 println!("🎯 Custom Benchmark Examples\n");
28
29 quick_calibrate_tsc_frequency();
30
31 // Benchmark 1: Simple arithmetic
32 println!("=== Arithmetic Operations ===");
33 SimpleBench::new("fast_multiply")
34 .bench(10000, || fast_multiply(12345, 67890))
35 .report();
36
37 // Benchmark 2: Recursive function (warning: slow!)
38 println!("\n=== Recursive Function ===");
39 SimpleBench::new("fibonacci_20")
40 .bench(100, || fibonacci(20)) // Only 100 iterations - fibonacci is slow!
41 .report();
42
43 // Benchmark 3: String operations
44 println!("\n=== String Operations ===");
45 SimpleBench::new("string_ops")
46 .bench(1000, || string_operations())
47 .report();
48
49 // Benchmark 4: Compare two implementations
50 println!("\n=== Implementation Comparison ===");
51 let method_a = SimpleBench::new("method_a")
52 .bench(5000, || {
53 // Method A: Manual loop
54 let mut sum = 0u64;
55 for i in 0..100 {
56 sum += i;
57 }
58 sum
59 })
60 .analyze();
61
62 let method_b = SimpleBench::new("method_b")
63 .bench(5000, || {
64 // Method B: Iterator
65 (0..100u64).sum::<u64>()
66 })
67 .analyze();
68
69 println!("Method A (manual loop): {}ns P99", method_a.p99);
70 println!("Method B (iterator): {}ns P99", method_b.p99);
71
72 if method_b.p99 < method_a.p99 {
73 let improvement = (method_a.p99 as f64 / method_b.p99 as f64 - 1.0) * 100.0;
74 println!("Iterator is {:.1}% faster!", improvement);
75 } else {
76 let degradation = (method_b.p99 as f64 / method_a.p99 as f64 - 1.0) * 100.0;
77 println!("Manual loop is {:.1}% faster!", degradation);
78 }
79
80 // Benchmark 5: Performance validation
81 println!("\n=== Performance Validation ===");
82 let critical_path = SimpleBench::new("critical_operation")
83 .bench(1000, || {
84 // Simulate a critical operation
85 std::hint::black_box(42 * 37 + 15)
86 })
87 .analyze();
88
89 const TARGET_P99_NS: u64 = 100;
90 if critical_path.meets_target(TARGET_P99_NS) {
91 println!("✅ Performance target met! P99: {}ns (target: <{}ns)",
92 critical_path.p99, TARGET_P99_NS);
93 } else {
94 println!("❌ Performance target missed! P99: {}ns (target: <{}ns)",
95 critical_path.p99, TARGET_P99_NS);
96 }
97}examples/real_comparison.rs (line 35)
27fn main() {
28 quick_calibrate_tsc_frequency();
29
30 println!("🔬 Real Algorithm Comparisons\n");
31
32 // Test 1: Sum algorithms
33 println!("=== Sum Algorithms (n=100) ===");
34
35 let loop_perf = SimpleBench::new("sum_with_loop")
36 .bench(10000, || sum_with_loop(100))
37 .analyze();
38
39 let formula_perf = SimpleBench::new("sum_with_formula")
40 .bench(10000, || sum_with_formula(100))
41 .analyze();
42
43 println!("Loop method: mean={}ns, P99={}ns", loop_perf.mean, loop_perf.p99);
44 println!("Formula method: mean={}ns, P99={}ns", formula_perf.mean, formula_perf.p99);
45
46 if formula_perf.mean < loop_perf.mean {
47 let improvement = (loop_perf.mean as f64 / formula_perf.mean as f64 - 1.0) * 100.0;
48 println!("✅ Formula is {:.1}% faster", improvement);
49 } else if loop_perf.mean < formula_perf.mean {
50 let degradation = (formula_perf.mean as f64 / loop_perf.mean as f64 - 1.0) * 100.0;
51 println!("⚠️ Loop is {:.1}% faster", degradation);
52 } else {
53 println!("🤝 Both methods perform similarly");
54 }
55
56 // Test 2: Even number check
57 println!("\n=== Even Number Check ===");
58
59 let modulo_perf = SimpleBench::new("is_even_modulo")
60 .bench(10000, || is_even_modulo(12345))
61 .analyze();
62
63 let bitwise_perf = SimpleBench::new("is_even_bitwise")
64 .bench(10000, || is_even_bitwise(12345))
65 .analyze();
66
67 println!("Modulo method: mean={}ns, P99={}ns", modulo_perf.mean, modulo_perf.p99);
68 println!("Bitwise method: mean={}ns, P99={}ns", bitwise_perf.mean, bitwise_perf.p99);
69
70 if bitwise_perf.mean < modulo_perf.mean {
71 let improvement = (modulo_perf.mean as f64 / bitwise_perf.mean as f64 - 1.0) * 100.0;
72 println!("✅ Bitwise is {:.1}% faster", improvement);
73 } else if modulo_perf.mean < bitwise_perf.mean {
74 let degradation = (bitwise_perf.mean as f64 / modulo_perf.mean as f64 - 1.0) * 100.0;
75 println!("⚠️ Modulo is {:.1}% faster", degradation);
76 } else {
77 println!("🤝 Both methods perform similarly");
78 }
79
80 // Test 3: Vector vs Array
81 println!("\n=== Vector vs Array Access ===");
82
83 let vec_data = vec![1, 2, 3, 4, 5];
84 let array_data = [1, 2, 3, 4, 5];
85
86 let vec_perf = SimpleBench::new("vector_access")
87 .bench(10000, || {
88 let sum = vec_data.iter().sum::<i32>();
89 std::hint::black_box(sum);
90 })
91 .analyze();
92
93 let array_perf = SimpleBench::new("array_access")
94 .bench(10000, || {
95 let sum = array_data.iter().sum::<i32>();
96 std::hint::black_box(sum);
97 })
98 .analyze();
99
100 println!("Vector access: mean={}ns, P99={}ns", vec_perf.mean, vec_perf.p99);
101 println!("Array access: mean={}ns, P99={}ns", array_perf.mean, array_perf.p99);
102
103 if array_perf.mean < vec_perf.mean {
104 let improvement = (vec_perf.mean as f64 / array_perf.mean as f64 - 1.0) * 100.0;
105 println!("✅ Array is {:.1}% faster", improvement);
106 } else if vec_perf.mean < array_perf.mean {
107 let degradation = (array_perf.mean as f64 / vec_perf.mean as f64 - 1.0) * 100.0;
108 println!("⚠️ Vector is {:.1}% faster", degradation);
109 } else {
110 println!("🤝 Both perform similarly");
111 }
112
113 println!("\n💡 Note: Results may vary between runs due to:");
114 println!(" - CPU cache state");
115 println!(" - System load");
116 println!(" - Compiler optimizations");
117 println!(" - Memory layout");
118}Sourcepub fn bench<F, R>(self, iterations: usize, f: F) -> Selfwhere
F: FnMut() -> R,
pub fn bench<F, R>(self, iterations: usize, f: F) -> Selfwhere
F: FnMut() -> R,
Examples found in repository?
examples/simple_benchmark_example.rs (lines 16-20)
5fn main() {
6 quick_calibrate_tsc_frequency();
7
8 #[cfg(target_arch = "aarch64")]
9 {
10 let resolution_ns = 1_000_000_000u64 / 24_000_000u64;
11 println!("Note: ARM64 timing resolution is ~{}ns. Very fast operations may show limited precision.\n", resolution_ns);
12 }
13
14 println!("=== Simple Arithmetic Benchmark ===");
15 SimpleBench::new("arithmetic")
16 .bench(10000, || {
17 let a = 42u64;
18 let b = 37u64;
19 a.wrapping_add(b).wrapping_mul(2)
20 })
21 .report();
22
23 println!("\n=== Vector Allocation Benchmark ===");
24 SimpleBench::new("vec_allocation")
25 .bench(1000, || {
26 let vec: Vec<u64> = Vec::with_capacity(100);
27 drop(vec);
28 })
29 .report();
30
31 println!("\n=== Function Call Overhead ===");
32 fn dummy_function(x: u64) -> u64 {
33 x.wrapping_add(1)
34 }
35
36 SimpleBench::new("function_call")
37 .bench(5000, || dummy_function(42))
38 .report();
39
40 println!("\n=== Custom Analysis Example ===");
41 let analysis = SimpleBench::new("analysis_example")
42 .bench(1000, || std::hint::black_box(42))
43 .analyze();
44
45 if analysis.meets_target(100) {
46 println!("✓ Performance target met! P99: {}ns", analysis.p99);
47 } else {
48 println!("✗ Performance target missed. P99: {}ns", analysis.p99);
49 }
50
51 println!("\n=== Implementation Comparison ===");
52
53 let old_analysis = SimpleBench::new("old_impl")
54 .bench(1000, || {
55 for _ in 0..10 {
56 std::hint::black_box(42);
57 }
58 })
59 .analyze();
60
61 let new_analysis = SimpleBench::new("new_impl")
62 .bench(1000, || {
63 std::hint::black_box(42);
64 })
65 .analyze();
66
67 println!("Old: {}ns P99, New: {}ns P99", old_analysis.p99, new_analysis.p99);
68
69 if new_analysis.mean == 0 {
70 println!("Improvement: Operation too fast to measure precisely (< {}ns resolution)",
71 1_000_000_000u64 / 24_000_000u64);
72 } else {
73 let improvement = (old_analysis.mean as f64 / new_analysis.mean as f64 - 1.0) * 100.0;
74 println!("Improvement: {:.1}% faster", improvement);
75 }
76}More examples
examples/custom_benchmark.rs (line 34)
26fn main() {
27 println!("🎯 Custom Benchmark Examples\n");
28
29 quick_calibrate_tsc_frequency();
30
31 // Benchmark 1: Simple arithmetic
32 println!("=== Arithmetic Operations ===");
33 SimpleBench::new("fast_multiply")
34 .bench(10000, || fast_multiply(12345, 67890))
35 .report();
36
37 // Benchmark 2: Recursive function (warning: slow!)
38 println!("\n=== Recursive Function ===");
39 SimpleBench::new("fibonacci_20")
40 .bench(100, || fibonacci(20)) // Only 100 iterations - fibonacci is slow!
41 .report();
42
43 // Benchmark 3: String operations
44 println!("\n=== String Operations ===");
45 SimpleBench::new("string_ops")
46 .bench(1000, || string_operations())
47 .report();
48
49 // Benchmark 4: Compare two implementations
50 println!("\n=== Implementation Comparison ===");
51 let method_a = SimpleBench::new("method_a")
52 .bench(5000, || {
53 // Method A: Manual loop
54 let mut sum = 0u64;
55 for i in 0..100 {
56 sum += i;
57 }
58 sum
59 })
60 .analyze();
61
62 let method_b = SimpleBench::new("method_b")
63 .bench(5000, || {
64 // Method B: Iterator
65 (0..100u64).sum::<u64>()
66 })
67 .analyze();
68
69 println!("Method A (manual loop): {}ns P99", method_a.p99);
70 println!("Method B (iterator): {}ns P99", method_b.p99);
71
72 if method_b.p99 < method_a.p99 {
73 let improvement = (method_a.p99 as f64 / method_b.p99 as f64 - 1.0) * 100.0;
74 println!("Iterator is {:.1}% faster!", improvement);
75 } else {
76 let degradation = (method_b.p99 as f64 / method_a.p99 as f64 - 1.0) * 100.0;
77 println!("Manual loop is {:.1}% faster!", degradation);
78 }
79
80 // Benchmark 5: Performance validation
81 println!("\n=== Performance Validation ===");
82 let critical_path = SimpleBench::new("critical_operation")
83 .bench(1000, || {
84 // Simulate a critical operation
85 std::hint::black_box(42 * 37 + 15)
86 })
87 .analyze();
88
89 const TARGET_P99_NS: u64 = 100;
90 if critical_path.meets_target(TARGET_P99_NS) {
91 println!("✅ Performance target met! P99: {}ns (target: <{}ns)",
92 critical_path.p99, TARGET_P99_NS);
93 } else {
94 println!("❌ Performance target missed! P99: {}ns (target: <{}ns)",
95 critical_path.p99, TARGET_P99_NS);
96 }
97}examples/real_comparison.rs (line 36)
27fn main() {
28 quick_calibrate_tsc_frequency();
29
30 println!("🔬 Real Algorithm Comparisons\n");
31
32 // Test 1: Sum algorithms
33 println!("=== Sum Algorithms (n=100) ===");
34
35 let loop_perf = SimpleBench::new("sum_with_loop")
36 .bench(10000, || sum_with_loop(100))
37 .analyze();
38
39 let formula_perf = SimpleBench::new("sum_with_formula")
40 .bench(10000, || sum_with_formula(100))
41 .analyze();
42
43 println!("Loop method: mean={}ns, P99={}ns", loop_perf.mean, loop_perf.p99);
44 println!("Formula method: mean={}ns, P99={}ns", formula_perf.mean, formula_perf.p99);
45
46 if formula_perf.mean < loop_perf.mean {
47 let improvement = (loop_perf.mean as f64 / formula_perf.mean as f64 - 1.0) * 100.0;
48 println!("✅ Formula is {:.1}% faster", improvement);
49 } else if loop_perf.mean < formula_perf.mean {
50 let degradation = (formula_perf.mean as f64 / loop_perf.mean as f64 - 1.0) * 100.0;
51 println!("⚠️ Loop is {:.1}% faster", degradation);
52 } else {
53 println!("🤝 Both methods perform similarly");
54 }
55
56 // Test 2: Even number check
57 println!("\n=== Even Number Check ===");
58
59 let modulo_perf = SimpleBench::new("is_even_modulo")
60 .bench(10000, || is_even_modulo(12345))
61 .analyze();
62
63 let bitwise_perf = SimpleBench::new("is_even_bitwise")
64 .bench(10000, || is_even_bitwise(12345))
65 .analyze();
66
67 println!("Modulo method: mean={}ns, P99={}ns", modulo_perf.mean, modulo_perf.p99);
68 println!("Bitwise method: mean={}ns, P99={}ns", bitwise_perf.mean, bitwise_perf.p99);
69
70 if bitwise_perf.mean < modulo_perf.mean {
71 let improvement = (modulo_perf.mean as f64 / bitwise_perf.mean as f64 - 1.0) * 100.0;
72 println!("✅ Bitwise is {:.1}% faster", improvement);
73 } else if modulo_perf.mean < bitwise_perf.mean {
74 let degradation = (bitwise_perf.mean as f64 / modulo_perf.mean as f64 - 1.0) * 100.0;
75 println!("⚠️ Modulo is {:.1}% faster", degradation);
76 } else {
77 println!("🤝 Both methods perform similarly");
78 }
79
80 // Test 3: Vector vs Array
81 println!("\n=== Vector vs Array Access ===");
82
83 let vec_data = vec![1, 2, 3, 4, 5];
84 let array_data = [1, 2, 3, 4, 5];
85
86 let vec_perf = SimpleBench::new("vector_access")
87 .bench(10000, || {
88 let sum = vec_data.iter().sum::<i32>();
89 std::hint::black_box(sum);
90 })
91 .analyze();
92
93 let array_perf = SimpleBench::new("array_access")
94 .bench(10000, || {
95 let sum = array_data.iter().sum::<i32>();
96 std::hint::black_box(sum);
97 })
98 .analyze();
99
100 println!("Vector access: mean={}ns, P99={}ns", vec_perf.mean, vec_perf.p99);
101 println!("Array access: mean={}ns, P99={}ns", array_perf.mean, array_perf.p99);
102
103 if array_perf.mean < vec_perf.mean {
104 let improvement = (vec_perf.mean as f64 / array_perf.mean as f64 - 1.0) * 100.0;
105 println!("✅ Array is {:.1}% faster", improvement);
106 } else if vec_perf.mean < array_perf.mean {
107 let degradation = (array_perf.mean as f64 / vec_perf.mean as f64 - 1.0) * 100.0;
108 println!("⚠️ Vector is {:.1}% faster", degradation);
109 } else {
110 println!("🤝 Both perform similarly");
111 }
112
113 println!("\n💡 Note: Results may vary between runs due to:");
114 println!(" - CPU cache state");
115 println!(" - System load");
116 println!(" - Compiler optimizations");
117 println!(" - Memory layout");
118}Sourcepub fn report(self)
pub fn report(self)
Examples found in repository?
examples/simple_benchmark_example.rs (line 21)
5fn main() {
6 quick_calibrate_tsc_frequency();
7
8 #[cfg(target_arch = "aarch64")]
9 {
10 let resolution_ns = 1_000_000_000u64 / 24_000_000u64;
11 println!("Note: ARM64 timing resolution is ~{}ns. Very fast operations may show limited precision.\n", resolution_ns);
12 }
13
14 println!("=== Simple Arithmetic Benchmark ===");
15 SimpleBench::new("arithmetic")
16 .bench(10000, || {
17 let a = 42u64;
18 let b = 37u64;
19 a.wrapping_add(b).wrapping_mul(2)
20 })
21 .report();
22
23 println!("\n=== Vector Allocation Benchmark ===");
24 SimpleBench::new("vec_allocation")
25 .bench(1000, || {
26 let vec: Vec<u64> = Vec::with_capacity(100);
27 drop(vec);
28 })
29 .report();
30
31 println!("\n=== Function Call Overhead ===");
32 fn dummy_function(x: u64) -> u64 {
33 x.wrapping_add(1)
34 }
35
36 SimpleBench::new("function_call")
37 .bench(5000, || dummy_function(42))
38 .report();
39
40 println!("\n=== Custom Analysis Example ===");
41 let analysis = SimpleBench::new("analysis_example")
42 .bench(1000, || std::hint::black_box(42))
43 .analyze();
44
45 if analysis.meets_target(100) {
46 println!("✓ Performance target met! P99: {}ns", analysis.p99);
47 } else {
48 println!("✗ Performance target missed. P99: {}ns", analysis.p99);
49 }
50
51 println!("\n=== Implementation Comparison ===");
52
53 let old_analysis = SimpleBench::new("old_impl")
54 .bench(1000, || {
55 for _ in 0..10 {
56 std::hint::black_box(42);
57 }
58 })
59 .analyze();
60
61 let new_analysis = SimpleBench::new("new_impl")
62 .bench(1000, || {
63 std::hint::black_box(42);
64 })
65 .analyze();
66
67 println!("Old: {}ns P99, New: {}ns P99", old_analysis.p99, new_analysis.p99);
68
69 if new_analysis.mean == 0 {
70 println!("Improvement: Operation too fast to measure precisely (< {}ns resolution)",
71 1_000_000_000u64 / 24_000_000u64);
72 } else {
73 let improvement = (old_analysis.mean as f64 / new_analysis.mean as f64 - 1.0) * 100.0;
74 println!("Improvement: {:.1}% faster", improvement);
75 }
76}More examples
examples/custom_benchmark.rs (line 35)
26fn main() {
27 println!("🎯 Custom Benchmark Examples\n");
28
29 quick_calibrate_tsc_frequency();
30
31 // Benchmark 1: Simple arithmetic
32 println!("=== Arithmetic Operations ===");
33 SimpleBench::new("fast_multiply")
34 .bench(10000, || fast_multiply(12345, 67890))
35 .report();
36
37 // Benchmark 2: Recursive function (warning: slow!)
38 println!("\n=== Recursive Function ===");
39 SimpleBench::new("fibonacci_20")
40 .bench(100, || fibonacci(20)) // Only 100 iterations - fibonacci is slow!
41 .report();
42
43 // Benchmark 3: String operations
44 println!("\n=== String Operations ===");
45 SimpleBench::new("string_ops")
46 .bench(1000, || string_operations())
47 .report();
48
49 // Benchmark 4: Compare two implementations
50 println!("\n=== Implementation Comparison ===");
51 let method_a = SimpleBench::new("method_a")
52 .bench(5000, || {
53 // Method A: Manual loop
54 let mut sum = 0u64;
55 for i in 0..100 {
56 sum += i;
57 }
58 sum
59 })
60 .analyze();
61
62 let method_b = SimpleBench::new("method_b")
63 .bench(5000, || {
64 // Method B: Iterator
65 (0..100u64).sum::<u64>()
66 })
67 .analyze();
68
69 println!("Method A (manual loop): {}ns P99", method_a.p99);
70 println!("Method B (iterator): {}ns P99", method_b.p99);
71
72 if method_b.p99 < method_a.p99 {
73 let improvement = (method_a.p99 as f64 / method_b.p99 as f64 - 1.0) * 100.0;
74 println!("Iterator is {:.1}% faster!", improvement);
75 } else {
76 let degradation = (method_b.p99 as f64 / method_a.p99 as f64 - 1.0) * 100.0;
77 println!("Manual loop is {:.1}% faster!", degradation);
78 }
79
80 // Benchmark 5: Performance validation
81 println!("\n=== Performance Validation ===");
82 let critical_path = SimpleBench::new("critical_operation")
83 .bench(1000, || {
84 // Simulate a critical operation
85 std::hint::black_box(42 * 37 + 15)
86 })
87 .analyze();
88
89 const TARGET_P99_NS: u64 = 100;
90 if critical_path.meets_target(TARGET_P99_NS) {
91 println!("✅ Performance target met! P99: {}ns (target: <{}ns)",
92 critical_path.p99, TARGET_P99_NS);
93 } else {
94 println!("❌ Performance target missed! P99: {}ns (target: <{}ns)",
95 critical_path.p99, TARGET_P99_NS);
96 }
97}Sourcepub fn analyze(self) -> BenchmarkAnalysis
pub fn analyze(self) -> BenchmarkAnalysis
Examples found in repository?
examples/simple_benchmark_example.rs (line 43)
5fn main() {
6 quick_calibrate_tsc_frequency();
7
8 #[cfg(target_arch = "aarch64")]
9 {
10 let resolution_ns = 1_000_000_000u64 / 24_000_000u64;
11 println!("Note: ARM64 timing resolution is ~{}ns. Very fast operations may show limited precision.\n", resolution_ns);
12 }
13
14 println!("=== Simple Arithmetic Benchmark ===");
15 SimpleBench::new("arithmetic")
16 .bench(10000, || {
17 let a = 42u64;
18 let b = 37u64;
19 a.wrapping_add(b).wrapping_mul(2)
20 })
21 .report();
22
23 println!("\n=== Vector Allocation Benchmark ===");
24 SimpleBench::new("vec_allocation")
25 .bench(1000, || {
26 let vec: Vec<u64> = Vec::with_capacity(100);
27 drop(vec);
28 })
29 .report();
30
31 println!("\n=== Function Call Overhead ===");
32 fn dummy_function(x: u64) -> u64 {
33 x.wrapping_add(1)
34 }
35
36 SimpleBench::new("function_call")
37 .bench(5000, || dummy_function(42))
38 .report();
39
40 println!("\n=== Custom Analysis Example ===");
41 let analysis = SimpleBench::new("analysis_example")
42 .bench(1000, || std::hint::black_box(42))
43 .analyze();
44
45 if analysis.meets_target(100) {
46 println!("✓ Performance target met! P99: {}ns", analysis.p99);
47 } else {
48 println!("✗ Performance target missed. P99: {}ns", analysis.p99);
49 }
50
51 println!("\n=== Implementation Comparison ===");
52
53 let old_analysis = SimpleBench::new("old_impl")
54 .bench(1000, || {
55 for _ in 0..10 {
56 std::hint::black_box(42);
57 }
58 })
59 .analyze();
60
61 let new_analysis = SimpleBench::new("new_impl")
62 .bench(1000, || {
63 std::hint::black_box(42);
64 })
65 .analyze();
66
67 println!("Old: {}ns P99, New: {}ns P99", old_analysis.p99, new_analysis.p99);
68
69 if new_analysis.mean == 0 {
70 println!("Improvement: Operation too fast to measure precisely (< {}ns resolution)",
71 1_000_000_000u64 / 24_000_000u64);
72 } else {
73 let improvement = (old_analysis.mean as f64 / new_analysis.mean as f64 - 1.0) * 100.0;
74 println!("Improvement: {:.1}% faster", improvement);
75 }
76}More examples
examples/custom_benchmark.rs (line 60)
26fn main() {
27 println!("🎯 Custom Benchmark Examples\n");
28
29 quick_calibrate_tsc_frequency();
30
31 // Benchmark 1: Simple arithmetic
32 println!("=== Arithmetic Operations ===");
33 SimpleBench::new("fast_multiply")
34 .bench(10000, || fast_multiply(12345, 67890))
35 .report();
36
37 // Benchmark 2: Recursive function (warning: slow!)
38 println!("\n=== Recursive Function ===");
39 SimpleBench::new("fibonacci_20")
40 .bench(100, || fibonacci(20)) // Only 100 iterations - fibonacci is slow!
41 .report();
42
43 // Benchmark 3: String operations
44 println!("\n=== String Operations ===");
45 SimpleBench::new("string_ops")
46 .bench(1000, || string_operations())
47 .report();
48
49 // Benchmark 4: Compare two implementations
50 println!("\n=== Implementation Comparison ===");
51 let method_a = SimpleBench::new("method_a")
52 .bench(5000, || {
53 // Method A: Manual loop
54 let mut sum = 0u64;
55 for i in 0..100 {
56 sum += i;
57 }
58 sum
59 })
60 .analyze();
61
62 let method_b = SimpleBench::new("method_b")
63 .bench(5000, || {
64 // Method B: Iterator
65 (0..100u64).sum::<u64>()
66 })
67 .analyze();
68
69 println!("Method A (manual loop): {}ns P99", method_a.p99);
70 println!("Method B (iterator): {}ns P99", method_b.p99);
71
72 if method_b.p99 < method_a.p99 {
73 let improvement = (method_a.p99 as f64 / method_b.p99 as f64 - 1.0) * 100.0;
74 println!("Iterator is {:.1}% faster!", improvement);
75 } else {
76 let degradation = (method_b.p99 as f64 / method_a.p99 as f64 - 1.0) * 100.0;
77 println!("Manual loop is {:.1}% faster!", degradation);
78 }
79
80 // Benchmark 5: Performance validation
81 println!("\n=== Performance Validation ===");
82 let critical_path = SimpleBench::new("critical_operation")
83 .bench(1000, || {
84 // Simulate a critical operation
85 std::hint::black_box(42 * 37 + 15)
86 })
87 .analyze();
88
89 const TARGET_P99_NS: u64 = 100;
90 if critical_path.meets_target(TARGET_P99_NS) {
91 println!("✅ Performance target met! P99: {}ns (target: <{}ns)",
92 critical_path.p99, TARGET_P99_NS);
93 } else {
94 println!("❌ Performance target missed! P99: {}ns (target: <{}ns)",
95 critical_path.p99, TARGET_P99_NS);
96 }
97}examples/real_comparison.rs (line 37)
27fn main() {
28 quick_calibrate_tsc_frequency();
29
30 println!("🔬 Real Algorithm Comparisons\n");
31
32 // Test 1: Sum algorithms
33 println!("=== Sum Algorithms (n=100) ===");
34
35 let loop_perf = SimpleBench::new("sum_with_loop")
36 .bench(10000, || sum_with_loop(100))
37 .analyze();
38
39 let formula_perf = SimpleBench::new("sum_with_formula")
40 .bench(10000, || sum_with_formula(100))
41 .analyze();
42
43 println!("Loop method: mean={}ns, P99={}ns", loop_perf.mean, loop_perf.p99);
44 println!("Formula method: mean={}ns, P99={}ns", formula_perf.mean, formula_perf.p99);
45
46 if formula_perf.mean < loop_perf.mean {
47 let improvement = (loop_perf.mean as f64 / formula_perf.mean as f64 - 1.0) * 100.0;
48 println!("✅ Formula is {:.1}% faster", improvement);
49 } else if loop_perf.mean < formula_perf.mean {
50 let degradation = (formula_perf.mean as f64 / loop_perf.mean as f64 - 1.0) * 100.0;
51 println!("⚠️ Loop is {:.1}% faster", degradation);
52 } else {
53 println!("🤝 Both methods perform similarly");
54 }
55
56 // Test 2: Even number check
57 println!("\n=== Even Number Check ===");
58
59 let modulo_perf = SimpleBench::new("is_even_modulo")
60 .bench(10000, || is_even_modulo(12345))
61 .analyze();
62
63 let bitwise_perf = SimpleBench::new("is_even_bitwise")
64 .bench(10000, || is_even_bitwise(12345))
65 .analyze();
66
67 println!("Modulo method: mean={}ns, P99={}ns", modulo_perf.mean, modulo_perf.p99);
68 println!("Bitwise method: mean={}ns, P99={}ns", bitwise_perf.mean, bitwise_perf.p99);
69
70 if bitwise_perf.mean < modulo_perf.mean {
71 let improvement = (modulo_perf.mean as f64 / bitwise_perf.mean as f64 - 1.0) * 100.0;
72 println!("✅ Bitwise is {:.1}% faster", improvement);
73 } else if modulo_perf.mean < bitwise_perf.mean {
74 let degradation = (bitwise_perf.mean as f64 / modulo_perf.mean as f64 - 1.0) * 100.0;
75 println!("⚠️ Modulo is {:.1}% faster", degradation);
76 } else {
77 println!("🤝 Both methods perform similarly");
78 }
79
80 // Test 3: Vector vs Array
81 println!("\n=== Vector vs Array Access ===");
82
83 let vec_data = vec![1, 2, 3, 4, 5];
84 let array_data = [1, 2, 3, 4, 5];
85
86 let vec_perf = SimpleBench::new("vector_access")
87 .bench(10000, || {
88 let sum = vec_data.iter().sum::<i32>();
89 std::hint::black_box(sum);
90 })
91 .analyze();
92
93 let array_perf = SimpleBench::new("array_access")
94 .bench(10000, || {
95 let sum = array_data.iter().sum::<i32>();
96 std::hint::black_box(sum);
97 })
98 .analyze();
99
100 println!("Vector access: mean={}ns, P99={}ns", vec_perf.mean, vec_perf.p99);
101 println!("Array access: mean={}ns, P99={}ns", array_perf.mean, array_perf.p99);
102
103 if array_perf.mean < vec_perf.mean {
104 let improvement = (vec_perf.mean as f64 / array_perf.mean as f64 - 1.0) * 100.0;
105 println!("✅ Array is {:.1}% faster", improvement);
106 } else if vec_perf.mean < array_perf.mean {
107 let degradation = (array_perf.mean as f64 / vec_perf.mean as f64 - 1.0) * 100.0;
108 println!("⚠️ Vector is {:.1}% faster", degradation);
109 } else {
110 println!("🤝 Both perform similarly");
111 }
112
113 println!("\n💡 Note: Results may vary between runs due to:");
114 println!(" - CPU cache state");
115 println!(" - System load");
116 println!(" - Compiler optimizations");
117 println!(" - Memory layout");
118}Auto Trait Implementations§
impl Freeze for SimpleBench
impl RefUnwindSafe for SimpleBench
impl Send for SimpleBench
impl Sync for SimpleBench
impl Unpin for SimpleBench
impl UnsafeUnpin for SimpleBench
impl UnwindSafe for SimpleBench
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more