Guest User

Untitled

a guest
May 27th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. use std::time::Instant;
  2.  
  3. const SCALE: usize = 100;
  4.  
  5. fn main() {
  6. let mut boxes: Vec<Box<Foo>> = Vec::with_capacity(SCALE);
  7. for _ in 0..SCALE {
  8. boxes.push(Box::new(Bar));
  9. }
  10. let mut bars: Vec<Bar> = Vec::with_capacity(SCALE);
  11. for _ in 0..SCALE {
  12. bars.push(Bar);
  13. }
  14. let start = Instant::now();
  15. for i in 0..SCALE {
  16. assert_eq!(boxes[i].foo(), 4)
  17. }
  18. let time = start.elapsed();
  19. println!("Box time: {:?}", time);
  20. let start = Instant::now();
  21. for i in 0..SCALE {
  22. assert_eq!(bars[i].foo(), 4)
  23. }
  24. let time = start.elapsed();
  25. println!("No box time: {:?}", time);
  26. }
  27.  
  28. trait Foo {
  29. fn foo(&self) -> i32 {
  30. 2 + 2
  31. }
  32. }
  33.  
  34. struct Bar;
  35.  
  36. impl Foo for Bar {}
Add Comment
Please, Sign In to add comment