Last active
May 3, 2021 19:07
-
-
Save glommer/58cea685c9035371092755466c531680 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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