1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#![feature(test)]
extern crate test;
use std::sync::Barrier;
use crossbeam_utils::atomic::AtomicCell;
use crossbeam_utils::thread;
#[bench]
fn load_u8(b: &mut test::Bencher) {
let a = AtomicCell::new(0u8);
let mut sum = 0;
b.iter(|| sum += a.load());
test::black_box(sum);
}
#[bench]
fn store_u8(b: &mut test::Bencher) {
let a = AtomicCell::new(0u8);
b.iter(|| a.store(1));
}
#[bench]
fn fetch_add_u8(b: &mut test::Bencher) {
let a = AtomicCell::new(0u8);
b.iter(|| a.fetch_add(1));
}
#[bench]
fn compare_exchange_u8(b: &mut test::Bencher) {
let a = AtomicCell::new(0u8);
let mut i = 0;
b.iter(|| {
let _ = a.compare_exchange(i, i.wrapping_add(1));
i = i.wrapping_add(1);
});
}
#[bench]
fn concurrent_load_u8(b: &mut test::Bencher) {
const THREADS: usize = 2;
const STEPS: usize = 1_000_000;
let start = Barrier::new(THREADS + 1);
let end = Barrier::new(THREADS + 1);
let exit = AtomicCell::new(false);
let a = AtomicCell::new(0u8);
thread::scope(|scope| {
for _ in 0..THREADS {
scope.spawn(|_| loop {
start.wait();
let mut sum = 0;
for _ in 0..STEPS {
sum += a.load();
}
test::black_box(sum);
end.wait();
if exit.load() {
break;
}
});
}
start.wait();
end.wait();
b.iter(|| {
start.wait();
end.wait();
});
start.wait();
exit.store(true);
end.wait();
})
.unwrap();
}
#[bench]
fn load_usize(b: &mut test::Bencher) {
let a = AtomicCell::new(0usize);
let mut sum = 0;
b.iter(|| sum += a.load());
test::black_box(sum);
}
#[bench]
fn store_usize(b: &mut test::Bencher) {
let a = AtomicCell::new(0usize);
b.iter(|| a.store(1));
}
#[bench]
fn fetch_add_usize(b: &mut test::Bencher) {
let a = AtomicCell::new(0usize);
b.iter(|| a.fetch_add(1));
}
#[bench]
fn compare_exchange_usize(b: &mut test::Bencher) {
let a = AtomicCell::new(0usize);
let mut i = 0;
b.iter(|| {
let _ = a.compare_exchange(i, i.wrapping_add(1));
i = i.wrapping_add(1);
});
}
#[bench]
fn concurrent_load_usize(b: &mut test::Bencher) {
const THREADS: usize = 2;
const STEPS: usize = 1_000_000;
let start = Barrier::new(THREADS + 1);
let end = Barrier::new(THREADS + 1);
let exit = AtomicCell::new(false);
let a = AtomicCell::new(0usize);
thread::scope(|scope| {
for _ in 0..THREADS {
scope.spawn(|_| loop {
start.wait();
let mut sum = 0;
for _ in 0..STEPS {
sum += a.load();
}
test::black_box(sum);
end.wait();
if exit.load() {
break;
}
});
}
start.wait();
end.wait();
b.iter(|| {
start.wait();
end.wait();
});
start.wait();
exit.store(true);
end.wait();
})
.unwrap();
}
|