Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(test, asm)]
- extern crate test;
- use test::Bencher;
- use std::alloc::{alloc, dealloc, alloc_zeroed, Layout};
- const SIZE: usize = 4096;
- #[bench]
- fn bench_alloc_memset_4096(b: &mut Bencher) {
- let layout = Layout::from_size_align(SIZE, 1).unwrap();
- b.iter(|| unsafe {
- let x = alloc(layout);
- std::ptr::write_bytes(x, 0, layout.size());
- asm!("" : : "m"(x) : :"volatile");
- std::ptr::write_bytes(x, 1, layout.size());
- asm!("" : : "m"(x) : :"volatile");
- dealloc(x, layout);
- });
- }
- #[bench]
- fn bench_alloc_4096(b: &mut Bencher) {
- let layout = Layout::from_size_align(SIZE, 1).unwrap();
- b.iter(|| unsafe {
- let x = alloc(layout);
- std::ptr::write_bytes(x, 1, layout.size());
- asm!("" : : "m"(x) : :"volatile");
- dealloc(x, layout);
- });
- }
- #[bench]
- fn bench_alloc_zeroed_4096(b: &mut Bencher) {
- let layout = Layout::from_size_align(SIZE, 1).unwrap();
- b.iter(|| unsafe {
- let x = alloc_zeroed(layout);
- std::ptr::write_bytes(x, 1, layout.size());
- asm!("" : : "m"(x) : :"volatile");
- dealloc(x, layout);
- });
- }
- /*
- rustc +nightly -O --test foo.rs && ./foo --bench
- running 3 tests
- test bench_alloc_4096 ... bench: 131 ns/iter (+/- 22)
- test bench_alloc_memset_4096 ... bench: 213 ns/iter (+/- 15)
- test bench_alloc_zeroed_4096 ... bench: 222 ns/iter (+/- 18)
- test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured; 0 filtered out
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement