Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::time::Instant;
- const SCALE: usize = 100;
- fn main() {
- let mut boxes: Vec<Box<Foo>> = Vec::with_capacity(SCALE);
- for _ in 0..SCALE {
- boxes.push(Box::new(Bar));
- }
- let mut bars: Vec<Bar> = Vec::with_capacity(SCALE);
- for _ in 0..SCALE {
- bars.push(Bar);
- }
- let start = Instant::now();
- for i in 0..SCALE {
- assert_eq!(boxes[i].foo(), 4)
- }
- let time = start.elapsed();
- println!("Box time: {:?}", time);
- let start = Instant::now();
- for i in 0..SCALE {
- assert_eq!(bars[i].foo(), 4)
- }
- let time = start.elapsed();
- println!("No box time: {:?}", time);
- }
- trait Foo {
- fn foo(&self) -> i32 {
- 2 + 2
- }
- }
- struct Bar;
- impl Foo for Bar {}
Add Comment
Please, Sign In to add comment