Advertisement
Guest User

Untitled

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