Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #![feature(test, asm)]
  2. extern crate test;
  3. use test::Bencher;
  4. use std::alloc::{alloc, dealloc, alloc_zeroed, Layout};
  5.  
  6.  
  7. #[bench]
  8. fn bench_alloc_memset_4096(b: &mut Bencher) {
  9. let layout = Layout::from_size_align(4096, 1).unwrap();
  10. b.iter(|| unsafe {
  11. let x = alloc(layout);
  12. std::ptr::write_bytes(x, 0, layout.size());
  13. *x = 1;
  14. asm!("" : : "m"(x) : :"volatile");
  15. dealloc(x, layout);
  16. });
  17. }
  18.  
  19.  
  20. #[bench]
  21. fn bench_alloc_zeroed_4096(b: &mut Bencher) {
  22. let layout = Layout::from_size_align(4096, 1).unwrap();
  23. b.iter(|| unsafe {
  24. let x = alloc_zeroed(layout);
  25. *x = 1;
  26. asm!("" : : "m"(x) : :"volatile");
  27. dealloc(x, layout);
  28. });
  29. }
  30.  
  31. /*
  32. rustc +nightly -O --test foo.rs && ./foo --bench
  33.  
  34. running 2 tests
  35. test bench_alloc_memset_4096 ... bench: 133 ns/iter (+/- 9)
  36. test bench_alloc_zeroed_4096 ... bench: 132 ns/iter (+/- 11)
  37. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement