Trying to bench mark allocation cost

I doing some experimenting with rust to better understand the value (if any) of pre-allocating data structures. However When I run my benchmark it is showing there is no difference between pre-allocated and not. I can't tell if this is just because rust is optimizing my code or if there is really no difference. Am I missing something here?

#![feature(test)]
extern crate test;

#[cfg(test)]
mod alloc {
    use test::Bencher;

    #[inline(never)]
    fn create_pre() -> Vec<u64> {
        Vec::with_capacity(1000000)
    }

    #[inline(never)]
    fn create_post() -> Vec<u64> {
        Vec::with_capacity(0)
    }

    #[bench]
    fn pre_alloc(b: &mut Bencher) {
        b.iter(|| {
            let mut arr = create_pre();
            for i in 0..=1000000 {
                arr.push(i);
            }
        })
    }

    #[bench]
    fn post_alloc(b: &mut Bencher) {
        b.iter(|| {
            let mut arr = create_post();
            for i in 0..=1000000 {
                arr.push(i);
            }
        })

    }
}

At a smaller size, like 100 elements, pre_alloc is nearly twice as fast as post_alloc, on my machine. But as the number of elements gets larger, the difference gets lost in the noise.

I believe this is because, at large sizes, the benchmark spends much more time pushing to the vector than reallocating it. At one million elements, the number of reallocations (less than twenty) is much smaller than the number of writes. And at large sizes, realloc is cheaper than you might expect. So the extra cost from needing to reallocate is too small to measure, in this specific case.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.