Skip to content

Instantly share code, notes, and snippets.

@glommer
Last active May 3, 2021 19:07
Show Gist options
  • Save glommer/58cea685c9035371092755466c531680 to your computer and use it in GitHub Desktop.
Save glommer/58cea685c9035371092755466c531680 to your computer and use it in GitHub Desktop.
use std::{rc::Rc, time::Instant};
struct Foo {
val: usize,
}
impl Foo {
fn new(val: usize) -> Self {
Foo { val }
}
}
fn main() {
let original = Foo::new(0);
let runs = 1_000_000_000;
let start = Instant::now();
{
let mut vec_ref: Vec<&Foo> = Vec::with_capacity(runs);
for _ in 0..runs {
vec_ref.push(&original);
}
} // the vector is destroyed here.
println!("Took {:#?}", start.elapsed());
let orig_rc = Rc::new(original);
let start = Instant::now();
{
let mut vec_ref: Vec<Rc<Foo>> = Vec::with_capacity(runs);
for _ in 0..runs {
vec_ref.push(orig_rc.clone());
}
} // the vector is destroyed here.
println!("Took {:#?}", start.elapsed());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment