Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 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. #[bench]
  7. fn bench_alloc_4096(b: &mut Bencher) {
  8. let layout = Layout::from_size_align(4096, 1).unwrap();
  9. b.iter(|| unsafe {
  10. let x = alloc(layout);
  11. *x = 1;
  12. asm!("" : : "m"(x) : :"volatile");
  13. dealloc(x, layout);
  14. });
  15. }
  16.  
  17. #[bench]
  18. fn bench_alloc_memset_4096(b: &mut Bencher) {
  19. let layout = Layout::from_size_align(4096, 1).unwrap();
  20. b.iter(|| unsafe {
  21. let x = alloc(layout);
  22. std::ptr::write_bytes(x, 0, layout.size());
  23. *x = 1;
  24. asm!("" : : "m"(x) : :"volatile");
  25. dealloc(x, layout);
  26. });
  27. }
  28.  
  29.  
  30. #[bench]
  31. fn bench_alloc_zeroed_4096(b: &mut Bencher) {
  32. let layout = Layout::from_size_align(4096, 1).unwrap();
  33. b.iter(|| unsafe {
  34. let x = alloc_zeroed(layout);
  35. *x = 1;
  36. asm!("" : : "m"(x) : :"volatile");
  37. dealloc(x, layout);
  38. });
  39. }
  40.  
  41. /*
  42. rustc +nightly -O --test foo.rs && ./foo --bench
  43.  
  44. running 3 tests
  45. test bench_alloc_4096 ... bench: 47 ns/iter (+/- 11)
  46. test bench_alloc_memset_4096 ... bench: 133 ns/iter (+/- 9)
  47. test bench_alloc_zeroed_4096 ... bench: 137 ns/iter (+/- 23)
  48.  
  49. test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured; 0 filtered out
  50.  
  51. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement