To answer your question, you can refer to the test box, which contains a solution for benchmarking (this requires nightly):
#![feature(test)] extern crate test; use std::rc::Rc; fn test_with_box() { let b = Box::new(1.0); test::black_box(*b * 2.0); } fn test_with_rc() { let rc = Rc::new(1.0); test::black_box(*rc * 2.0); } #[bench] fn bench_box(b: &mut test::Bencher) { b.iter(test_with_box) } #[bench] fn bench_rc(b: &mut test::Bencher) { b.iter(test_with_rc) }
Now compile and run this:
$ rustc --test -O rc.rs && ./rc --bench running 2 tests test bench_box ... bench: 22 ns/iter (+/- 0) test bench_rc ... bench: 22 ns/iter (+/- 0) test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
What happened? Obviously, the RC was completely composed. As it should be, because we did not clone . Therefore, changing the corresponding fn to:
fn test_with_rc() { let rc = Rc::new(1.0); test::black_box(*rc.clone() * 2.0); }
We get the following:
running 2 tests test bench_box ... bench: 23 ns/iter (+/- 1) test bench_rc ... bench: 25 ns/iter (+/- 1) test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured
So, suffice it to say that you will probably have to worry about other things before looking at the RC-induced overhead.
llogiq
source share