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
//! Benchmarks for random distributions in NumRS2
//!
//! This benchmark suite compares the performance of:
//! 1. NumRS2's native distribution implementations
//! 2. SciRS2 integration implementations (when available)
//!
//! Run with: `cargo bench --bench bench_distributions`
//! To include SciRS2: `cargo bench --bench bench_distributions --features scirs`
#![allow(deprecated)]
#![allow(clippy::result_large_err)]
#[macro_use]
extern crate criterion;
use criterion::{BenchmarkId, Criterion};
use numrs2::array::Array;
use numrs2::random::distributions::*;
use std::hint::black_box;
// Import the SciRS2 integration when available
#[cfg(feature = "scirs")]
use numrs2::interop::scirs_compat as sci;
fn bench_normal_distributions(c: &mut Criterion) {
let mut group = c.benchmark_group("Normal Distributions");
// Sample sizes to benchmark
let sizes = [100, 1000, 10000];
for size in sizes.iter() {
// NumRS2 normal distribution
group.bench_with_input(BenchmarkId::new("NumRS2 normal", size), size, |b, &size| {
b.iter(|| black_box(normal(0.0, 1.0, &[size])));
});
// NumRS2 truncated normal distribution
group.bench_with_input(
BenchmarkId::new("NumRS2 truncated_normal", size),
size,
|b, &size| {
b.iter(|| black_box(truncated_normal(0.0, 1.0, -2.0, 2.0, &[size])));
},
);
// SciRS2 truncated normal distribution (when available)
#[cfg(feature = "scirs")]
group.bench_with_input(
BenchmarkId::new("SciRS2 truncated_normal", size),
size,
|b, &size| {
b.iter(|| black_box(sci::truncated_normal(0.0, 1.0, -2.0, 2.0, &[size])));
},
);
}
group.finish();
}
fn bench_advanced_distributions(c: &mut Criterion) {
let mut group = c.benchmark_group("Advanced Distributions");
// Sample sizes to benchmark
let sizes = [100, 1000, 10000];
for size in sizes.iter() {
// Native NumRS2 chi-square
group.bench_with_input(
BenchmarkId::new("NumRS2 chisquare", size),
size,
|b, &size| {
b.iter(|| black_box(chisquare(5.0, &[size])));
},
);
// NumRS2 noncentral chi-square distribution (when available)
#[cfg(not(feature = "scirs"))]
group.bench_with_input(
BenchmarkId::new("NumRS2 noncentral_chisquare", size),
size,
|b, &size| {
b.iter(|| black_box(noncentral_chisquare(5.0, 2.0, &[size])));
},
);
// SciRS2 noncentral chi-square distribution (when available)
#[cfg(feature = "scirs")]
group.bench_with_input(
BenchmarkId::new("SciRS2 noncentral_chisquare", size),
size,
|b, &size| {
b.iter(|| black_box(sci::noncentral_chisquare(5.0, 2.0, &[size])));
},
);
// Native NumRS2 F distribution
// TODO: f_dist is not yet implemented
// group.bench_with_input(BenchmarkId::new("NumRS2 f", size), size, |b, &size| {
// b.iter(|| black_box(f_dist(5.0, 10.0, &[size])));
// });
// NumRS2 noncentral F distribution (when available)
#[cfg(not(feature = "scirs"))]
group.bench_with_input(
BenchmarkId::new("NumRS2 noncentral_f", size),
size,
|b, &size| {
b.iter(|| black_box(noncentral_f(5.0, 10.0, 2.0, &[size])));
},
);
// SciRS2 noncentral F distribution (when available)
#[cfg(feature = "scirs")]
group.bench_with_input(
BenchmarkId::new("SciRS2 noncentral_f", size),
size,
|b, &size| {
b.iter(|| black_box(sci::noncentral_f(5.0, 10.0, 2.0, &[size])));
},
);
}
group.finish();
}
fn bench_circular_distributions(c: &mut Criterion) {
let mut group = c.benchmark_group("Circular Distributions");
// Sample sizes to benchmark
let sizes = [100, 1000, 10000];
for size in sizes.iter() {
// NumRS2 uniform circular distribution
group.bench_with_input(
BenchmarkId::new("NumRS2 uniform circular", size),
size,
|b, &size| {
b.iter(|| black_box(uniform(0.0, 2.0 * std::f64::consts::PI, &[size])));
},
);
// NumRS2 von Mises distribution (when available)
#[cfg(not(feature = "scirs"))]
group.bench_with_input(
BenchmarkId::new("NumRS2 vonmises", size),
size,
|b, &size| {
b.iter(|| black_box(vonmises(0.0, 2.0, &[size])));
},
);
// SciRS2 von Mises distribution (when available)
#[cfg(feature = "scirs")]
group.bench_with_input(
BenchmarkId::new("SciRS2 vonmises", size),
size,
|b, &size| {
b.iter(|| black_box(sci::vonmises(0.0, 2.0, &[size])));
},
);
}
group.finish();
}
fn bench_multivariate_distributions(c: &mut Criterion) {
let mut group = c.benchmark_group("Multivariate Distributions");
// Sample sizes to benchmark
let sizes = [10, 100, 1000];
// Common setup for multivariate tests
let mean = vec![0.0, 0.0];
let cov_data = vec![1.0, 0.5, 0.5, 1.0];
let cov = Array::from_vec(cov_data).reshape(&[2, 2]);
// Create a rotation matrix for 45 degrees
use std::f64::consts::FRAC_1_SQRT_2;
let rotation_data = vec![FRAC_1_SQRT_2, FRAC_1_SQRT_2, -FRAC_1_SQRT_2, FRAC_1_SQRT_2];
let rotation = Array::from_vec(rotation_data.clone()).reshape(&[2, 2]);
for size in sizes.iter() {
// Native NumRS2 multivariate normal
group.bench_with_input(
BenchmarkId::new("NumRS2 multivariate_normal", size),
size,
|b, &size| {
b.iter(|| black_box(multivariate_normal(&mean, &cov, Some(&[size]))));
},
);
// NumRS2 multivariate normal with Cholesky decomposition
// TODO: multivariate_normal_cholesky is not yet implemented
// group.bench_with_input(
// BenchmarkId::new("NumRS2 multivariate_normal_cholesky", size),
// size,
// |b, &size| {
// b.iter(|| black_box(multivariate_normal_cholesky(&mean, &cov, size)));
// },
// );
// SciRS2 multivariate normal with rotation (when available)
#[cfg(feature = "scirs")]
group.bench_with_input(
BenchmarkId::new("SciRS2 multivariate_normal_with_rotation", size),
size,
|b, &size| {
b.iter(|| {
black_box(sci::multivariate_normal_with_rotation(
&mean,
&cov,
Some(&[size]),
Some(&rotation),
))
});
},
);
}
group.finish();
}
criterion_group!(
benches,
bench_normal_distributions,
bench_advanced_distributions,
bench_circular_distributions,
bench_multivariate_distributions
);
criterion_main!(benches);