Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 /*!
2 A lightweight micro-benchmarking library which:
3
4 * uses linear regression to screen off constant error;
5 * handles benchmarks which mutate state;
6 * can measure simple polynomial or exponential scaling behavior
7 * is very easy to use!
8
9 `scaling` is designed to work with either slow or fast functions.
10 It's forked from [easybench], which is itself inspired by [criterion],
11 but doesn't do as much sophisticated
12 analysis (no outlier detection, no HTML output).
13
14 [easybench]: https://crates.io/crates/easybench
15 [criterion]: https://crates.io/crates/criterion
16
17 ```
18 use scaling::{bench,bench_env,bench_scaling};
19
20 # fn fib(_: usize) -> usize { 0 }
21 #
22 // Simple benchmarks are performed with `bench` or `bench_scaling`.
23 println!("fib 200: {}", bench(|| fib(200) ));
24 println!("fib 500: {}", bench(|| fib(500) ));
25 println!("fib scaling: {}", bench_scaling(|n| fib(n), 0));
26
27 // If a function needs to mutate some state, use `bench_env`.
28 println!("reverse: {}", bench_env(vec![0;100], |xs| xs.reverse() ));
29 println!("sort: {}", bench_env(vec![0;100], |xs| xs.sort() ));
30 ```
31
32 Running the above yields the following results:
33
34 ```none
35 fib 200: 50ns (R²=0.995, 20435 iterations in 68 samples)
36 fib 500: 144ns (R²=0.999, 7235 iterations in 57 samples)
37 fib scaling: 0.30ns/N (R²=0.999, 8645 iterations in 59 samples)
38 reverse: 46ns (R²=0.990, 30550 iterations in 72 samples)
39 sort: 137ns (R²=0.991, 187129 iterations in 91 samples)
40 ```
41
42 Easy! However, please read the [caveats](#caveats) below before using.
43
44 # Benchmarking algorithm
45
46 An *iteration* is a single execution of your code. A *sample* is a measurement,
47 during which your code may be run many times. In other words: taking a sample
48 means performing some number of iterations and measuring the total time.
49
50 The first sample we take performs only 1 iteration, but as we continue
51 taking samples we increase the number of iterations with increasing
52 rapidity. We
53 stop either when a global time limit is reached (currently 10 seconds),
54 or when we have collected sufficient statistics (but have run for at
55 least a millisecond).
56
57 If a benchmark requires some state to run, `n` copies of the initial state are
58 prepared before the sample is taken.
59
60 Once we have the data, we perform OLS linear regression to find out how
61 the sample time varies with the number of iterations in the sample. The
62 gradient of the regression line tells us how long it takes to perform a
63 single iteration of the benchmark. The R² value is a measure of how much
64 noise there is in the data.
65
66 If the function is too slow (5 or 10 seconds), the linear regression is skipped,
67 and a simple average of timings is used. For slow functions, any overhead will
68 be negligible.
69
70 # Caveats
71
72 ## Caveat 1: Harness overhead
73
74 **TL;DR: Compile with `--release`; the overhead is likely to be within the
75 **noise of your
76 benchmark.**
77
78 Any work which `scaling` does once-per-sample is ignored (this is the purpose of the linear
79 regression technique described above). However, work which is done once-per-iteration *will* be
80 counted in the final times.
81
82 * In the case of [`bench()`] this amounts to incrementing the loop counter and
83 [copying the return value](#bonus-caveat-black-box).
84 * In the case of [`bench_env`] and [`bench_gen_env`], we also do a lookup into a big vector in
85 order to get the environment for that iteration.
86 * If you compile your program unoptimised, there may be additional overhead.
87
88 The cost of the above operations depend on the details of your benchmark;
89 namely: (1) how large is the return value? and (2) does the benchmark evict
90 the environment vector from the CPU cache? In practice, these criteria are only
91 satisfied by longer-running benchmarks, making these effects hard to measure.
92
93 ## Caveat 2: Pure functions
94
95 **TL;DR: Return enough information to prevent the optimiser from eliminating
96 code from your benchmark.**
97
98 Benchmarking pure functions involves a nasty gotcha which users should be
99 aware of. Consider the following benchmarks:
100
101 ```
102 # use scaling::{bench,bench_env};
103 #
104 # fn fib(_: usize) -> usize { 0 }
105 #
106 let fib_1 = bench(|| fib(500) ); // fine
107 let fib_2 = bench(|| { fib(500); } ); // spoiler: NOT fine
108 let fib_3 = bench_env(0, |x| { *x = fib(500); } ); // also fine, but ugly
109 # let _ = (fib_1, fib_2, fib_3);
110 ```
111
112 The results are a little surprising:
113
114 ```none
115 fib_1: 110 ns (R²=1.000, 9131585 iterations in 144 samples)
116 fib_2: 0 ns (R²=1.000, 413289203 iterations in 184 samples)
117 fib_3: 109 ns (R²=1.000, 9131585 iterations in 144 samples)
118 ```
119
120 Oh, `fib_2`, why do you lie? The answer is: `fib(500)` is pure, and its
121 return value is immediately thrown away, so the optimiser replaces the call
122 with a no-op (which clocks in at 0 ns).
123
124 What about the other two? `fib_1` looks very similar, with one exception:
125 the closure which we're benchmarking returns the result of the `fib(500)`
126 call. When it runs your code, `scaling` takes the return value and tricks the
127 optimiser into thinking it's going to use it for something, before throwing
128 it away. This is why `fib_1` is safe from having code accidentally eliminated.
129
130 In the case of `fib_3`, we actually *do* use the return value: each
131 iteration we take the result of `fib(500)` and store it in the iteration's
132 environment. This has the desired effect, but looks a bit weird.
133
134 ## Bonus caveat: Black box
135
136 The function which `scaling` uses to trick the optimiser (`black_box`)
137 is stolen from [bencher], which [states]:
138
139 [bencher]: https://docs.rs/bencher/
140 [states]: https://docs.rs/bencher/0.1.2/bencher/fn.black_box.html
141
142 > NOTE: We don't have a proper black box in stable Rust. This is a workaround
143 > implementation, that may have a too big performance overhead, depending on
144 > operation, or it may fail to properly avoid having code optimized out. It
145 > is good enough that it is used by default.
146 */
147
148 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/;
149 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::{selfrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/, Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#};
150 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::*;
151
152 // We try to spend at very most this many seconds (roughly) in total on
153 // each benchmark.
154 const BENCH_TIME_MAX_DESPERATIONrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX_DESPERATION.: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# = Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(120);
155 // We try to spend at most this many seconds (roughly) in total on
156 // each benchmark.
157 const BENCH_TIME_MAXrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX.: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# = Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(10);
158 // We try to spend at least this many seconds in total on each
159 // benchmark.
160 const BENCH_TIME_MINrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MIN.: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration# = Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(1);
161
162 /// Statistics for a benchmark run.
163 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
164 pub struct Statsrust-analyzer cargo scaling 0.1.3 Stats# {
165 /// The time, in nanoseconds, per iteration. If the benchmark generated
166 /// fewer than 2 samples in the allotted time then this will be NaN.
167 pub ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter.: f64,
168 /// The coefficient of determination, R².
169 ///
170 /// This is an indication of how noisy the benchmark was, where 1 is
171 /// good and 0 is bad. Be suspicious of values below 0.9.
172 pub goodness_of_fitrust-analyzer cargo scaling 0.1.3 Stats#goodness_of_fit.: f64,
173 /// How many times the benchmarked code was actually run.
174 pub iterationsrust-analyzer cargo scaling 0.1.3 Stats#iterations.: usize,
175 /// How many samples were taken (ie. how many times we allocated the
176 /// environment and measured the time).
177 pub samplesrust-analyzer cargo scaling 0.1.3 Stats#samples.: usize,
178 }
179
180 impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Statsrust-analyzer cargo scaling 0.1.3 Stats# {
181 fn fmtrust-analyzer cargo scaling 0.1.3 impl#[Stats][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
182 if self.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter..is_nanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]is_nan().() {
183 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
184 f,
185 "Only generated {} sample(s) - we can't fit a regression line to that! \
186 Try making your benchmark faster.",
187 self.samplesrust-analyzer cargo scaling 0.1.3 Stats#samples.
188 )
189 } else {
190 let per_iter = Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_nanos().(self.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter. as u64);
191 let per_iter = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", per_iter);
192 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
193 f,
194 "{:>11} (R²={:.3}, {} iterations in {} samples)",
195 per_iter, self.goodness_of_fitrust-analyzer cargo scaling 0.1.3 Stats#goodness_of_fit., self.iterationsrust-analyzer cargo scaling 0.1.3 Stats#iterations., self.samplesrust-analyzer cargo scaling 0.1.3 Stats#samples.
196 )
197 }
198 }
199 }
200
201 /// Run a benchmark.
202 ///
203 /// The return value of `f` is not used, but we trick the optimiser into
204 /// thinking we're going to use it. Make sure to return enough information
205 /// to prevent the optimiser from eliminating code from your benchmark! (See
206 /// the module docs for more.)
207 pub fn benchrust-analyzer cargo scaling 0.1.3 bench().<F, O>(mut f: F) -> Statsrust-analyzer cargo scaling 0.1.3 Stats#
208 where
209 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#() -> O,
210 {
211 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().((), |_| f())
212 }
213
214 /// Run a benchmark with an environment.
215 ///
216 /// The value `env` is a clonable prototype for the "benchmark
217 /// environment". Each iteration receives a freshly-cloned mutable copy of
218 /// this environment. The time taken to clone the environment is not included
219 /// in the results.
220 ///
221 /// Nb: it's very possible that we will end up allocating many (>10,000)
222 /// copies of `env` at the same time. Probably best to keep it small.
223 ///
224 /// See `bench` and the module docs for more.
225 ///
226 /// ## Overhead
227 ///
228 /// Every iteration, `bench_env` performs a lookup into a big vector in
229 /// order to get the environment for that iteration. If your benchmark
230 /// is memory-intensive then this could, in the worst case, amount to a
231 /// systematic cache-miss (ie. this vector would have to be fetched from
232 /// DRAM at the start of every iteration). In this case the results could be
233 /// affected by a hundred nanoseconds. This is a worst-case scenario however,
234 /// and I haven't actually been able to trigger it in practice... but it's
235 /// good to be aware of the possibility.
236 pub fn bench_envrust-analyzer cargo scaling 0.1.3 bench_env().<F, I, O>(env: I, f: F) -> Statsrust-analyzer cargo scaling 0.1.3 Stats#
237 where
238 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut I) -> O,
239 I: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#,
240 {
241 bench_gen_envrust-analyzer cargo scaling 0.1.3 bench_gen_env().(move || env.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(), f)
242 }
243
244 /// Run a benchmark with a generated environment.
245 ///
246 /// The function `gen_env` creates the "benchmark environment" for the
247 /// computation. Each iteration receives a freshly-created environment. The
248 /// time taken to create the environment is not included in the results.
249 ///
250 /// Nb: it's very possible that we will end up generating many (>10,000)
251 /// copies of `env` at the same time. Probably best to keep it small.
252 ///
253 /// See `bench` and the module docs for more.
254 ///
255 /// ## Overhead
256 ///
257 /// Every iteration, `bench_gen_env` performs a lookup into a big vector
258 /// in order to get the environment for that iteration. If your benchmark
259 /// is memory-intensive then this could, in the worst case, amount to a
260 /// systematic cache-miss (ie. this vector would have to be fetched from
261 /// DRAM at the start of every iteration). In this case the results could be
262 /// affected by a hundred nanoseconds. This is a worst-case scenario however,
263 /// and I haven't actually been able to trigger it in practice... but it's
264 /// good to be aware of the possibility.
265 pub fn bench_gen_envrust-analyzer cargo scaling 0.1.3 bench_gen_env().<G, F, I, O>(mut gen_env: G, mut f: F) -> Statsrust-analyzer cargo scaling 0.1.3 Stats#
266 where
267 G: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#() -> I,
268 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&mut I) -> O,
269 {
270 let mut data = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
271 // The time we started the benchmark (not used in results)
272 let bench_start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
273
274 // Collect data until BENCH_TIME_MAX is reached.
275 for iters in slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(BENCH_SCALE_TIMErust-analyzer cargo scaling 0.1.3 BENCH_SCALE_TIME.) {
276 // Prepare the environments - one per iteration
277 let mut xs = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeat_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat_with/repeat_with().(&mut gen_env)
278 .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(iters)
279 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<I>>();
280 // Start the clock
281 let iter_start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
282 // We iterate over `&mut xs` rather than draining it, because we
283 // don't want to drop the env values until after the clock has stopped.
284 for x in &mut xs {
285 // Run the code and pretend to use the output
286 pretend_to_userust-analyzer cargo scaling 0.1.3 pretend_to_use().(f(x));
287 }
288 let time = iter_start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
289 data.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((iters, time));
290
291 let elapsed = bench_start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
292 if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MINrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MIN. && data.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().3 {
293 // If the first iter in a sample is consistently slow, that's fine -
294 // that's why we do the linear regression. If the first sample is slower
295 // than the rest, however, that's not fine. Therefore, we discard the
296 // first sample as a cache-warming exercise.
297
298 // Compute some stats
299 let (grad, r2) = regressionrust-analyzer cargo scaling 0.1.3 regression().(&data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().);
300 let stats = Statsrust-analyzer cargo scaling 0.1.3 Stats# {
301 ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter.: grad,
302 goodness_of_fitrust-analyzer cargo scaling 0.1.3 Stats#goodness_of_fit.: r2,
303 iterationsrust-analyzer cargo scaling 0.1.3 Stats#iterations.: data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _)| x).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().(),
304 samplesrust-analyzer cargo scaling 0.1.3 Stats#samples.: data[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(),
305 };
306 if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MAXrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX. || r2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().0.99 {
307 return stats;
308 }
309 } else if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MAXrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX. {
310 let total_time: f64 = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, t)| t.as_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_nanos().() as f64).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().();
311 let iterations = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _)| x).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().();
312 return Statsrust-analyzer cargo scaling 0.1.3 Stats# {
313 ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter.: total_time rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().iterations as f64,
314 iterationsrust-analyzer cargo scaling 0.1.3 Stats#iterations.,
315 goodness_of_fitrust-analyzer cargo scaling 0.1.3 Stats#goodness_of_fit.: 0.0,
316 samplesrust-analyzer cargo scaling 0.1.3 Stats#samples.: data.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(),
317 };
318 }
319 }
320 unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!()
321 }
322
323 /// Statistics for a benchmark run determining the scaling of a function.
324 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
325 pub struct ScalingStatsrust-analyzer cargo scaling 0.1.3 ScalingStats# {
326 pub scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling.: Scalingrust-analyzer cargo scaling 0.1.3 Scaling#,
327 pub goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit.: f64,
328 /// How many times the benchmarked code was actually run.
329 pub iterationsrust-analyzer cargo scaling 0.1.3 ScalingStats#iterations.: usize,
330 /// How many samples were taken (ie. how many times we allocated the
331 /// environment and measured the time).
332 pub samplesrust-analyzer cargo scaling 0.1.3 ScalingStats#samples.: usize,
333 }
334 /// The timing and scaling results (without statistics) for a benchmark.
335 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
336 pub struct Scalingrust-analyzer cargo scaling 0.1.3 Scaling# {
337 /// The scaling power
338 /// If this is 2, for instance, you have an O(N²) algorithm.
339 pub powerrust-analyzer cargo scaling 0.1.3 Scaling#power.: usize,
340 /// An exponetial behavior, i.e. 2ᴺ
341 pub exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.: usize,
342 /// The time, in nanoseconds, per scaled size of the problem. If
343 /// the problem scales as O(N²) for instance, this is the number
344 /// of nanoseconds per N².
345 pub ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale.: f64,
346 }
347
348 impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for ScalingStatsrust-analyzer cargo scaling 0.1.3 ScalingStats# {
349 fn fmtrust-analyzer cargo scaling 0.1.3 impl#[ScalingStats][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
350 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
351 f,
352 "{} (R²={:.3}, {} iterations in {} samples)",
353 self.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling., self.goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit., self.iterationsrust-analyzer cargo scaling 0.1.3 ScalingStats#iterations., self.samplesrust-analyzer cargo scaling 0.1.3 ScalingStats#samples.
354 )
355 }
356 }
357 impl Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Scalingrust-analyzer cargo scaling 0.1.3 Scaling# {
358 fn fmtrust-analyzer cargo scaling 0.1.3 impl#[Scaling][Display]fmt().(&self, f: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
359 let per_iter = Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_nanos().(self.ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. as u64);
360 let per_iter = if self.ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt().1.0 {
361 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:.2}ns", self.ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale.)
362 } else if self.ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt().10.0 {
363 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:.1}ns", self.ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale.)
364 } else {
365 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", per_iter)
366 };
367 if self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 {
368 match self.powerrust-analyzer cargo scaling 0.1.3 Scaling#power. {
369 0 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/iter", per_iter),
370 1 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N ", per_iter),
371 2 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N² ", per_iter),
372 3 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N³ ", per_iter),
373 4 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N⁴ ", per_iter),
374 5 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N⁵ ", per_iter),
375 6 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N⁶ ", per_iter),
376 7 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N⁷ ", per_iter),
377 8 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N⁸ ", per_iter),
378 9 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N⁹ ", per_iter),
379 _ => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/N^{}", per_iter, self.powerrust-analyzer cargo scaling 0.1.3 Scaling#power.),
380 }
381 } else {
382 match self.powerrust-analyzer cargo scaling 0.1.3 Scaling#power. {
383 0 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/{}ᴺ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
384 1 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
385 2 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N²{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
386 3 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N³{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
387 4 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N⁴{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
388 5 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N⁵{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
389 6 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N⁶{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
390 7 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N⁷{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
391 8 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N⁸{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
392 9 => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N⁹{}ᴺ) ", per_iter, self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
393 _ => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "{:>8}/(N^{}{}ᴺ)", per_iter, self.powerrust-analyzer cargo scaling 0.1.3 Scaling#power., self.exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.),
394 }
395 }
396 }
397 }
398
399 /// Benchmark the power-law scaling of the function
400 ///
401 /// This function assumes that the function scales as 𝑶(𝑁ᴾ𝐸ᴺ).
402 /// It conisders higher powers for faster functions, and tries to
403 /// keep the measuring time around 10s. It measures the power ᴾ and exponential base 𝐸
404 /// based on n R² goodness of fit parameter.
405 pub fn bench_scalingrust-analyzer cargo scaling 0.1.3 bench_scaling().<F, O>(f: F, nmin: usize) -> ScalingStatsrust-analyzer cargo scaling 0.1.3 ScalingStats#
406 where
407 F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(usize) -> O,
408 {
409 let mut data = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
410 // The time we started the benchmark (not used in results)
411 let bench_start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
412
413 // Collect data until BENCH_TIME_MAX is reached.
414 for iters in slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(BENCH_SCALE_TIMErust-analyzer cargo scaling 0.1.3 BENCH_SCALE_TIME.) {
415 // Prepare the environments - nmin per iteration
416 let n = if nmin rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { iters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().nmin } else { iters };
417 // Generate a Vec holding n's to hopefully keep the optimizer
418 // from lifting the function out of the loop, as it could if
419 // we had `f(n)` in there, and `f` were inlined or `const`.
420 let xs = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![n; iters];
421 // Start the clock
422 let iter_start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
423 for x in xs.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() {
424 // Run the code and pretend to use the output
425 pretend_to_userust-analyzer cargo scaling 0.1.3 pretend_to_use().(f(x));
426 }
427 let time = iter_start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
428 data.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((n, iters, time));
429
430 let elapsed = bench_start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
431 if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MINrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MIN. {
432 let stats = compute_scaling_genrust-analyzer cargo scaling 0.1.3 compute_scaling_gen().(&data);
433 if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MAX_DESPERATIONrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX_DESPERATION.
434 || (elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MAXrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX. && stats.goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().0.0)
435 || stats.goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().0.99
436 {
437 return stats;
438 }
439 }
440 }
441 unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!()
442 }
443
444 /// Benchmark the power-law scaling of the function with generated input
445 ///
446 /// This function is like [`bench_scaling`], but uses a generating function
447 /// to construct the input to your benchmarked function.
448 ///
449 /// This function assumes that the function scales as 𝑶(𝑁ᴾ𝐸ᴺ).
450 /// It conisders higher powers for faster functions, and tries to
451 /// keep the measuring time around 10s. It measures the power ᴾ and exponential base 𝐸
452 /// based on n R² goodness of fit parameter.
453 ///
454 /// # Example
455 /// ```
456 /// use scaling::bench_scaling_gen;
457 ///
458 /// let summation = bench_scaling_gen(|n| vec![3.0; n], |v| v.iter().cloned().sum::<f64>(),0);
459 /// println!("summation: {}", summation);
460 /// assert_eq!(1, summation.scaling.power); // summation must run in linear time.
461 /// ```
462 /// which gives output
463 /// ```none
464 /// summation: 43ns/N (R²=0.996, 445 iterations in 29 samples)
465 /// ```
466 pub fn bench_scaling_genrust-analyzer cargo scaling 0.1.3 bench_scaling_gen().<G, F, I, O>(mut gen_env: G, f: F, nmin: usize) -> ScalingStatsrust-analyzer cargo scaling 0.1.3 ScalingStats#
467 where
468 G: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(usize) -> I,
469 F: Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#(&mut I) -> O,
470 {
471 let mut data = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
472 // The time we started the benchmark (not used in results)
473 let bench_start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
474
475 let mut am_slow = false;
476 // Collect data until BENCH_TIME_MAX is reached.
477 for iters in slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(BENCH_SCALE_TIMErust-analyzer cargo scaling 0.1.3 BENCH_SCALE_TIME.) {
478 // Prepare the environments - nmin per iteration
479 let n = if nmin rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { iters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().nmin } else { iters };
480 let iters = if am_slow { 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().(iters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`BitAnd<Self>`]bitand().1) } else { iters };
481 let mut xs = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::repeat_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/sources/repeat_with/repeat_with().(|| gen_env(n))
482 .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(iters)
483 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<I>>();
484 // Start the clock
485 let iter_start = Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().();
486 // We iterate over `&mut xs` rather than draining it, because we
487 // don't want to drop the env values until after the clock has stopped.
488 for x in &mut xs {
489 // Run the code and pretend to use the output
490 pretend_to_userust-analyzer cargo scaling 0.1.3 pretend_to_use().(f(x));
491 }
492 let time = iter_start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
493 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().am_slow && iters rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 && time rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_microsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_micros().(1) {
494 am_slow = true;
495 }
496 data.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((n, iters, time));
497
498 let elapsed = bench_start.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
499 if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MINrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MIN. {
500 let stats = compute_scaling_genrust-analyzer cargo scaling 0.1.3 compute_scaling_gen().(&data);
501 if elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MAX_DESPERATIONrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX_DESPERATION.
502 || (elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().BENCH_TIME_MAXrust-analyzer cargo scaling 0.1.3 BENCH_TIME_MAX. && stats.goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().0.0)
503 || stats.goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().0.99
504 {
505 return stats;
506 }
507 }
508 }
509 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("how did I get here?!");
510 unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!()
511 }
512
513 /// This function assumes that the function scales as 𝑶(𝑁ᴾ𝐸ᴺ). It measures the scaling
514 /// based on n R² goodness of fit parameter, and returns the best fit.
515 /// If it believes itself clueless, the goodness_of_fit is set to zero.
516 fn compute_scaling_genrust-analyzer cargo scaling 0.1.3 compute_scaling_gen().(data: &[(usize, usize, Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#)]) -> ScalingStatsrust-analyzer cargo scaling 0.1.3 ScalingStats# {
517 let num_n = {
518 let mut ns = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(n, _, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().n).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>();
519 ns.deduprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]dedup().();
520 ns.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()
521 };
522
523 // If the first iter in a sample is consistently slow, that's fine -
524 // that's why we do the linear regression. If the first sample is slower
525 // than the rest, however, that's not fine. Therefore, we discard the
526 // first sample as a cache-warming exercise.
527
528 // Compute some stats for each of several different
529 // powers, to see which seems most accurate.
530 let mut stats = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
531 let mut best = 0;
532 let mut second_best = 0;
533 for i in 1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#num_n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2 {
534 for power in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#i {
535 let exponential = i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().power;
536 let pdata: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().
537 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
538 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(n, i, t)| {
539 (
540 (exponential as f64).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(n as i32)
541 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().(n as f64).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(power as i32)
542 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().(i as f64),
543 t,
544 )
545 })
546 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
547 let (grad, r2) = fregressionrust-analyzer cargo scaling 0.1.3 fregression().(&pdata);
548 stats.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(ScalingStatsrust-analyzer cargo scaling 0.1.3 ScalingStats# {
549 scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling.: Scalingrust-analyzer cargo scaling 0.1.3 Scaling# {
550 powerrust-analyzer cargo scaling 0.1.3 Scaling#power.,
551 exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential.,
552 ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale.: grad,
553 },
554 goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit.: r2,
555 iterationsrust-analyzer cargo scaling 0.1.3 ScalingStats#iterations.: data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _, _)| x).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().(),
556 samplesrust-analyzer cargo scaling 0.1.3 ScalingStats#samples.: data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(),
557 });
558 if r2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt().stats[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().best]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. || stats[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().best]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit..is_nanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]is_nan().() {
559 second_best = best;
560 best = stats.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1;
561 }
562 }
563 }
564
565 if num_n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().10 || stats[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().second_best]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq().stats[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().best]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. {
566 stats[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().best]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()..goodness_of_fitrust-analyzer cargo scaling 0.1.3 ScalingStats#goodness_of_fit. = 0.0;
567 } else {
568 // println!("finished...");
569 // for s in stats.iter() {
570 // println!(" {}", s);
571 // }
572 // for d in data[data.len()-4..].iter() {
573 // println!(" {}, {} -> {} ns", d.0, d.1, d.2.as_nanos());
574 // }
575 // println!("best is {}", stats[best]);
576 }
577 stats[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().best]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()..clonerust-analyzer cargo scaling 0.1.3 impl#[ScalingStats][Clone]clone().()
578 }
579
580 /// Compute the OLS linear regression line for the given data set, returning
581 /// the line's gradient and R². Requires at least 2 samples.
582 //
583 // Overflows:
584 //
585 // * sum(x * x): num_samples <= 0.5 * log_k (1 + 2 ^ 64 (FACTOR - 1))
586 fn regressionrust-analyzer cargo scaling 0.1.3 regression().(data: &[(usize, Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#)]) -> (f64, f64) {
587 if data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().2 {
588 return (f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/NAN., f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/NAN.);
589 }
590 // Do all the arithmetic using f64, because it can happen that the
591 // squared numbers to overflow using integer arithmetic if the
592 // tests are too fast (so we run too many iterations).
593 let data: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = data
594 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
595 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, y)| (x as f64, y.as_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_nanos().() as f64))
596 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
597 let n = data.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() as f64;
598 let nxbar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _)| x).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>(); // iter_time > 5e-11 ns
599 let nybar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(_, y)| y).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>(); // TIME_LIMIT < 2 ^ 64 ns
600 let nxxbar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _)| x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().x).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>(); // num_iters < 13_000_000_000
601 let nyybar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(_, y)| y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().y).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>(); // TIME_LIMIT < 4.3 e9 ns
602 let nxybar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, y)| x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().y).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>();
603 let ncovar = nxybar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().((nxbar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().nybar) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n);
604 let nxvar = nxxbar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().((nxbar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().nxbar) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n);
605 let nyvar = nyybar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().((nybar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().nybar) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n);
606 let gradient = ncovar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().nxvar;
607 let r2 = (ncovar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().ncovar) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().(nxvar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().nyvar);
608 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r2.is_nanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]is_nan().() || r2 <= 1.0);
609 (gradient, r2)
610 }
611
612 /// Compute the OLS linear regression line for the given data set, returning
613 /// the line's gradient and R². Requires at least 2 samples.
614 //
615 // Overflows:
616 //
617 // * sum(x * x): num_samples <= 0.5 * log_k (1 + 2 ^ 64 (FACTOR - 1))
618 fn fregressionrust-analyzer cargo scaling 0.1.3 fregression().(data: &[(f64, Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#)]) -> (f64, f64) {
619 if data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().2 {
620 return (f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/NAN., f64rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/NAN.);
621 }
622 // Do all the arithmetic using f64, because it can happen that the
623 // squared numbers to overflow using integer arithmetic if the
624 // tests are too fast (so we run too many iterations).
625 let data: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = data
626 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
627 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, y)| (x as f64, y.as_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_nanos().() as f64))
628 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
629 let n = data.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() as f64;
630 let xbar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _)| x).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n;
631 let xvar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, _)| (x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().xbar).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(2)).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n;
632 let ybar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(_, y)| y).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n;
633 let yvar = data.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(_, y)| (y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().ybar).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(2)).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n;
634 let covar = data
635 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
636 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(x, y)| (x rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().xbar) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().(y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().ybar))
637 .sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<f64>()
638 /rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().n;
639 let gradient = covar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().xvar;
640 let r2 = covar.powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(2) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().(xvar rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().yvar);
641 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r2.is_nanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]is_nan().() || r2 <= 1.0);
642 (gradient, r2)
643 }
644
645 // Stolen from `bencher`, where it's known as `black_box`.
646 //
647 // NOTE: We don't have a proper black box in stable Rust. This is a workaround
648 // implementation, that may have a too big performance overhead, depending
649 // on operation, or it may fail to properly avoid having code optimized
650 // out. It is good enough that it is used by default.
651 //
652 // A function that is opaque to the optimizer, to allow benchmarks to pretend
653 // to use outputs to assist in avoiding dead-code elimination.
654 fn pretend_to_userust-analyzer cargo scaling 0.1.3 pretend_to_use().<T>(dummy: T) -> T {
655 unsafe {
656 let ret = ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::read_volatilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read_volatile().(&dummy);
657 ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::forgetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/forget().(dummy);
658 ret
659 }
660 }
661
662 #[cfg(test)]
663 mod testsrust-analyzer cargo scaling 0.1.3 tests/ {
664 use superrust-analyzer cargo scaling 0.1.3 crate/::*;
665 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/;
666 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#;
667
668 fn fibrust-analyzer cargo scaling 0.1.3 tests/fib().(n: usize) -> usize {
669 let mut i = 0;
670 let mut sum = 0;
671 let mut last = 0;
672 let mut curr = 1usize;
673 while i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1 {
674 sum = curr.wrapping_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]wrapping_add().(last);
675 last = curr;
676 curr = sum;
677 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
678 }
679 sum
680 }
681
682 // This is only here because doctests don't work with `--nocapture`.
683 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
684 #[ignore]
685 fn doctests_againrust-analyzer cargo scaling 0.1.3 tests/doctests_again().() {
686 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
687 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("fib 200: {}", benchrust-analyzer cargo scaling 0.1.3 bench().(|| fibrust-analyzer cargo scaling 0.1.3 tests/fib().(200)));
688 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("fib 500: {}", benchrust-analyzer cargo scaling 0.1.3 bench().(|| fibrust-analyzer cargo scaling 0.1.3 tests/fib().(500)));
689 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("fib scaling: {}", bench_scalingrust-analyzer cargo scaling 0.1.3 bench_scaling().(|n| fibrust-analyzer cargo scaling 0.1.3 tests/fib().(n), 0));
690 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("reverse: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; 100], |xs| xs.reverserust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]reverse().()));
691 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("sort: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0; 100], |xs| xs.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().()));
692
693 // This is fine:
694 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("fib 1: {}", benchrust-analyzer cargo scaling 0.1.3 bench().(|| fibrust-analyzer cargo scaling 0.1.3 tests/fib().(500)));
695 // This is NOT fine:
696 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
697 "fib 2: {}",
698 benchrust-analyzer cargo scaling 0.1.3 bench().(|| {
699 fibrust-analyzer cargo scaling 0.1.3 tests/fib().(500);
700 })
701 );
702 // This is also fine, but a bit weird:
703 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
704 "fib 3: {}",
705 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(0, |x| {
706 *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().x = fibrust-analyzer cargo scaling 0.1.3 tests/fib().(500);
707 })
708 );
709 }
710
711 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
712 fn scales_o_onerust-analyzer cargo scaling 0.1.3 tests/scales_o_one().() {
713 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
714 let stats = bench_scalingrust-analyzer cargo scaling 0.1.3 bench_scaling().(|_| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10)), 1);
715 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("O(N): {}", stats);
716 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..powerrust-analyzer cargo scaling 0.1.3 Scaling#power., 0);
717 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" error: {:e}", stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7);
718 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!((stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7).absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]abs().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). 1e6);
719 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}", stats).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("samples"));
720 }
721
722 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
723 fn scales_o_nrust-analyzer cargo scaling 0.1.3 tests/scales_o_n().() {
724 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
725 let stats = bench_scalingrust-analyzer cargo scaling 0.1.3 bench_scaling().(|n| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().n as u64)), 1);
726 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("O(N): {}", stats);
727 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..powerrust-analyzer cargo scaling 0.1.3 Scaling#power., 1);
728 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" error: {:e}", stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7);
729 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!((stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7).absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]abs().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). 1e5);
730
731 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Summing integers");
732 let stats = bench_scaling_genrust-analyzer cargo scaling 0.1.3 bench_scaling_gen().(
733 |n| (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#n as u64).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(),
734 |v| v.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u64>(),
735 1,
736 );
737 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("O(N): {}", stats);
738 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" error: {:e}", stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7);
739 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..powerrust-analyzer cargo scaling 0.1.3 Scaling#power., 1);
740 }
741
742 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
743 fn scales_o_n_log_n_looks_like_nrust-analyzer cargo scaling 0.1.3 tests/scales_o_n_log_n_looks_like_n().() {
744 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Sorting integers");
745 let stats = bench_scaling_genrust-analyzer cargo scaling 0.1.3 bench_scaling_gen().(
746 |n| {
747 (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#n as u64)
748 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| (i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().13 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().5) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Rem<Self>`]rem().137)
749 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>()
750 },
751 |v| v.sortrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort().(),
752 1,
753 );
754 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("O(N log N): {}", stats);
755 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" error: {:e}", stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7);
756 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..powerrust-analyzer cargo scaling 0.1.3 Scaling#power., 1);
757 }
758
759 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
760 fn scales_o_2_to_the_nrust-analyzer cargo scaling 0.1.3 tests/scales_o_2_to_the_n().() {
761 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
762 let stats = bench_scalingrust-analyzer cargo scaling 0.1.3 bench_scaling().(|n| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_nanosrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_nanos().((1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`Shl<usize>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`Shl<usize>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[i32][`Shl<usize>`]shl().n) as u64)), 1);
763 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("O(2ᴺ): {}", stats);
764 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..powerrust-analyzer cargo scaling 0.1.3 Scaling#power., 0);
765 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..exponentialrust-analyzer cargo scaling 0.1.3 Scaling#exponential., 2);
766 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" error: {:e}", stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1.0);
767 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!((stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1.0).absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]abs().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). 0.2);
768 }
769
770 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
771 fn scales_o_n_squarerust-analyzer cargo scaling 0.1.3 tests/scales_o_n_square().() {
772 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
773 let stats = bench_scalingrust-analyzer cargo scaling 0.1.3 bench_scaling().(
774 |n| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().(n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().n) as u64)),
775 1,
776 );
777 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("O(N): {}", stats);
778 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..powerrust-analyzer cargo scaling 0.1.3 Scaling#power., 2);
779 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" error: {:e}", stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7);
780 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!((stats.scalingrust-analyzer cargo scaling 0.1.3 ScalingStats#scaling..ns_per_scalerust-analyzer cargo scaling 0.1.3 Scaling#ns_per_scale. -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). 1e7).absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]abs().() <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]lt(). 1e5);
781 }
782
783 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
784 fn very_quickrust-analyzer cargo scaling 0.1.3 tests/very_quick().() {
785 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
786 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("very quick: {}", benchrust-analyzer cargo scaling 0.1.3 bench().(|| {}));
787 }
788
789 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
790 fn very_slowrust-analyzer cargo scaling 0.1.3 tests/very_slow().() {
791 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
792 let stats = benchrust-analyzer cargo scaling 0.1.3 bench().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(400)));
793 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("very slow: {}", stats);
794 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stats.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter. >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). 399.0e6);
795 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(3, stats.samplesrust-analyzer cargo scaling 0.1.3 Stats#samples.);
796 }
797
798 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
799 fn painfully_slowrust-analyzer cargo scaling 0.1.3 tests/painfully_slow().() {
800 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
801 let stats = benchrust-analyzer cargo scaling 0.1.3 bench().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(11)));
802 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("painfully slow: {}", stats);
803 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("ns {}", stats.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter.);
804 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stats.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter. >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). 11.0e9);
805 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(1, stats.iterationsrust-analyzer cargo scaling 0.1.3 Stats#iterations.);
806 }
807
808 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
809 fn sadly_slowrust-analyzer cargo scaling 0.1.3 tests/sadly_slow().() {
810 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
811 let stats = benchrust-analyzer cargo scaling 0.1.3 bench().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_secsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_secs().(6)));
812 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("sadly slow: {}", stats);
813 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("ns {}", stats.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter.);
814 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(stats.ns_per_iterrust-analyzer cargo scaling 0.1.3 Stats#ns_per_iter. >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialOrd<Self>`]gt(). 6.0e9);
815 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(2, stats.iterationsrust-analyzer cargo scaling 0.1.3 Stats#iterations.);
816 }
817
818 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
819 fn test_sleeprust-analyzer cargo scaling 0.1.3 tests/test_sleep().() {
820 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
821 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
822 "sleep 1 ms: {}",
823 benchrust-analyzer cargo scaling 0.1.3 bench().(|| threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::sleeprust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/sleep().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(1)))
824 );
825 }
826
827 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
828 fn nooprust-analyzer cargo scaling 0.1.3 tests/noop().() {
829 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
830 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("noop base: {}", benchrust-analyzer cargo scaling 0.1.3 bench().(|| {}));
831 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("noop 0: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 0], |_| {}));
832 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("noop 16: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 16], |_| {}));
833 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("noop 64: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 64], |_| {}));
834 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("noop 256: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 256], |_| {}));
835 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("noop 512: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 512], |_| {}));
836 }
837
838 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
839 fn ret_valuerust-analyzer cargo scaling 0.1.3 tests/ret_value().() {
840 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!();
841 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
842 "no ret 32: {}",
843 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 32], |x| { x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().() })
844 );
845 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("return 32: {}", bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 32], |x| x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().()));
846 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
847 "no ret 256: {}",
848 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 256], |x| { x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().() })
849 );
850 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
851 "return 256: {}",
852 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 256], |x| x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().())
853 );
854 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
855 "no ret 1024: {}",
856 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 1024], |x| { x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().() })
857 );
858 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
859 "return 1024: {}",
860 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 1024], |x| x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().())
861 );
862 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
863 "no ret 4096: {}",
864 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 4096], |x| { x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().() })
865 );
866 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
867 "return 4096: {}",
868 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 4096], |x| x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().())
869 );
870 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
871 "no ret 50000: {}",
872 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 50000], |x| { x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().() })
873 );
874 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(
875 "return 50000: {}",
876 bench_envrust-analyzer cargo scaling 0.1.3 bench_env().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u64; 50000], |x| x.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().())
877 );
878 }
879 }
880
881 // Each time we take a sample we increase the number of iterations
882 // using a slow version of the Fibonacci sequence, which
883 // asymptotically grows exponentially, but also gives us a different
884 // value each time (except for repeating 1 twice, once for warmup).
885
886 // For our standard `bench_*` we use slow_fib(25), which was chosen to
887 // asymptotically match the prior behavior of the library, which grew
888 // by an exponential of 1.1.
889 const BENCH_SCALE_TIMErust-analyzer cargo scaling 0.1.3 BENCH_SCALE_TIME.: usize = 25;
890
891 fn slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(scale_time: usize) -> impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = usize> {
892 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
893 struct SlowFibrust-analyzer cargo scaling 0.1.3 SlowFib# {
894 whichrust-analyzer cargo scaling 0.1.3 SlowFib#which.: usize,
895 bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>,
896 }
897 impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for SlowFibrust-analyzer cargo scaling 0.1.3 SlowFib# {
898 type Itemrust-analyzer cargo scaling 0.1.3 impl#[SlowFib][Iterator]Item# = usize;
899 fn nextrust-analyzer cargo scaling 0.1.3 impl#[SlowFib][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
900 // println!("!!! {:?}", self);
901 let oldwhich = self.whichrust-analyzer cargo scaling 0.1.3 SlowFib#which.;
902 self.whichrust-analyzer cargo scaling 0.1.3 SlowFib#which. = (self.whichrust-analyzer cargo scaling 0.1.3 SlowFib#which. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().self.bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
903 self.bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().self.whichrust-analyzer cargo scaling 0.1.3 SlowFib#which.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = self.bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().oldwhich]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().self.bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().self.whichrust-analyzer cargo scaling 0.1.3 SlowFib#which.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().;
904 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(self.bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().self.whichrust-analyzer cargo scaling 0.1.3 SlowFib#which.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().)
905 }
906 }
907 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(scale_time >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). 3);
908 let mut buffer = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![1; scale_time];
909 // buffer needs just the two zeros to make it start with two 1
910 // values. The rest should be 1s.
911 buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = 0;
912 buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().2]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = 0;
913 SlowFibrust-analyzer cargo scaling 0.1.3 SlowFib# { whichrust-analyzer cargo scaling 0.1.3 SlowFib#which.: 0, bufferrust-analyzer cargo scaling 0.1.3 SlowFib#buffer. }
914 }
915
916 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
917 fn test_fibrust-analyzer cargo scaling 0.1.3 test_fib().() {
918 // The following code was used to demonstrate that asymptotically
919 // the SlowFib grows as the 1.1 power, just as the old code. It
920 // differs in that it increases linearly at the beginning, which
921 // leads to larger numbers earlier in the sequence. It also
922 // differs in that it does not repeat any numbers in the sequence,
923 // which hopefully leads to better linear regression, particularly
924 // if we can only run a few iterations.
925 let mut prev = 1;
926 for x in slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(25).takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(200) {
927 let rat = x as f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().prev as f64;
928 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("ratio: {}/{} = {}", prev, x, rat);
929 prev = x;
930 }
931 let five: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(25).takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(5).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
932 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&five, &[1, 1, 2, 3, 4]);
933 let more: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = slow_fibrust-analyzer cargo scaling 0.1.3 slow_fib().(25).takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(32).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
934 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
935 &more,
936 &[
937 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
938 24, 25, 26, 28, 31, 35, 40, 46,
939 ]
940 );
941 let previous_sequence: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#32).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|n| (1.1f64).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(n).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]round().() as usize).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
942 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
943 &previous_sequence,
944 &[
945 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10, 11, 12, 13,
946 14, 16, 17, 19,
947 ]
948 );
949 let previous_sequence: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = (20..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#40)
950 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|n| (1.1f64).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]powi().(n).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]round().() as usize)
951 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
952 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
953 &previous_sequence,
954 &[7, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 26, 28, 31, 34, 37, 41,]
955 );
956 }