I have a friend benchmarking addition of two arrays in Ruby each with 100 million elements. I wanted to do the same in Rust and compare performance but I'm getting stack overflow errors if I go much further beyond 500 thousand items in each array.
Here are my two benchmarks that work:
#![feature(test)]
extern crate test;
use test::Bencher;
const LENGTH: usize = 500000;
#[bench]
fn add_replace(d: &mut Bencher) {
d.iter(|| {
let mut a = [2_u64; LENGTH];
let b = [3_u64; LENGTH];
for i in 0..LENGTH {
let v = a[i];
std::mem::replace(&mut a[i], v + b[i]);
}
});
}
#[bench]
fn add_push(d: &mut Bencher) {
d.iter(|| {
let a = [2_u64; LENGTH];
let b = [3_u64; LENGTH];
let mut c: Vec<u64> = Vec::with_capacity(LENGTH);
for i in 0..LENGTH {
c.push(a[i] + b[i]);
}
});
}
And the results:
running 2 tests
test add_push ... bench: 3,238,045 ns/iter (+/- 126,598)
test add_replace ... bench: 1,523,138 ns/iter (+/- 48,715)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out
But if you change the 500000
to 600000
I get this error on both benches:
running 2 tests
test add_push ...
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
error: An unknown error occurred
Running --verbose
offers no help at all here.
Can you help me get past these limitations? And can you help clarify what limitations I'm hitting here? Thanks!