История изменений
Исправление
RazrFalcon,
(текущая версия)
:
У меня так и не получилось обмануть оптимизатор, возможно кто-то подскажет.
#[macro_use] extern crate bencher;
use bencher::Bencher;
#[derive(Clone, Copy)]
struct MyError();
static NUMBERS: &[i32] = &[10, -20, 30, 0, -100, 50];
fn panics_impl() -> i32 {
std::panic::set_hook(Box::new(|_info| {}));
let mut total = 0;
for n in NUMBERS {
total += std::panic::catch_unwind(|| panics_1(*n)).unwrap_or(0);
}
total
}
#[inline(never)]
fn panics_1(n: i32) -> i32 {
panics_2(n)
}
#[inline(never)]
fn panics_2(n: i32) -> i32 {
panics_3(n)
}
#[inline(never)]
fn panics_3(n: i32) -> i32 {
panics_4(n)
}
#[inline(never)]
fn panics_4(n: i32) -> i32 {
if n > 0 {
n
} else {
panic!(MyError())
}
}
fn panics(bencher: &mut Bencher) {
bencher.iter(|| {
let _ = panics_impl();
})
}
fn errors_impl() -> i32 {
let mut total = 0;
for n in NUMBERS {
total += errors_1(*n).unwrap_or(0);
}
total
}
#[inline(never)]
fn errors_1(n: i32) -> Result<i32, MyError> {
let v = errors_2(n)?;
Ok(v)
}
#[inline(never)]
fn errors_2(n: i32) -> Result<i32, MyError> {
let v = errors_3(n)?;
Ok(v)
}
#[inline(never)]
fn errors_3(n: i32) -> Result<i32, MyError> {
let v = errors_4(n)?;
Ok(v)
}
#[inline(never)]
fn errors_4(n: i32) -> Result<i32, MyError> {
if n > 0 {
Ok(n)
} else {
Err(MyError())
}
}
fn errors(bencher: &mut Bencher) {
bencher.iter(|| {
let _ = errors_impl();
});
}
benchmark_group!(benches, errors, panics);
benchmark_main!(benches);
test errors ... bench: 0 ns/iter (+/- 0)
test panics ... bench: 9,430 ns/iter (+/- 48)
С бектрейсом:
test errors ... bench: 0 ns/iter (+/- 0)
test panics ... bench: 1,133,437 ns/iter (+/- 152,135)
Исправление
RazrFalcon,
:
У меня так и не получилось обмануть оптимизатор, возможно кто-то подскажет.
#[macro_use] extern crate bencher;
use bencher::Bencher;
#[derive(Clone, Copy)]
struct MyError();
static NUMBERS: &[i32] = &[10, -20, 30, 0, -100, 50];
fn panics_impl() -> i32 {
std::panic::set_hook(Box::new(|_info| {}));
let mut total = 0;
for n in NUMBERS {
total += std::panic::catch_unwind(|| panics_1(*n)).unwrap_or(0);
}
total
}
#[inline(never)]
fn panics_1(n: i32) -> i32 {
panics_2(n)
}
#[inline(never)]
fn panics_2(n: i32) -> i32 {
panics_3(n)
}
#[inline(never)]
fn panics_3(n: i32) -> i32 {
panics_4(n)
}
#[inline(never)]
fn panics_4(n: i32) -> i32 {
if n > 0 {
n
} else {
panic!(MyError())
}
}
fn panics(bencher: &mut Bencher) {
bencher.iter(|| {
let _ = panics_impl();
})
}
fn errors_impl() -> i32 {
let mut total = 0;
for n in NUMBERS {
total += errors_1(*n).unwrap_or(0);
}
total
}
#[inline(never)]
fn errors_1(n: i32) -> Result<i32, MyError> {
let v = errors_2(n)?;
Ok(v)
}
#[inline(never)]
fn errors_2(n: i32) -> Result<i32, MyError> {
let v = errors_3(n)?;
Ok(v)
}
#[inline(never)]
fn errors_3(n: i32) -> Result<i32, MyError> {
let v = errors_4(n)?;
Ok(v)
}
#[inline(never)]
fn errors_4(n: i32) -> Result<i32, MyError> {
if n > 0 {
Ok(n)
} else {
Err(MyError())
}
}
fn errors(bencher: &mut Bencher) {
bencher.iter(|| {
let _ = errors_impl();
});
}
benchmark_group!(benches, errors, panics);
benchmark_main!(benches);
test errors ... bench: 0 ns/iter (+/- 0)
test panics ... bench: 9,430 ns/iter (+/- 48)
С бектресом:
test errors ... bench: 0 ns/iter (+/- 0)
test panics ... bench: 1,133,437 ns/iter (+/- 152,135)
Исходная версия
RazrFalcon,
:
У меня так и не получилось обмануть оптимизатор, возможно кто-то подскажет.
#[macro_use] extern crate bencher;
use bencher::Bencher;
#[derive(Clone, Copy)]
struct MyError();
static NUMBERS: &[i32] = &[10, -20, 30, 0, -100, 50];
fn panics_impl() -> i32 {
std::panic::set_hook(Box::new(|_info| {}));
let mut total = 0;
for n in NUMBERS {
total += std::panic::catch_unwind(|| panics_1(*n)).unwrap_or(0);
}
total
}
#[inline(never)]
fn panics_1(n: i32) -> i32 {
panics_2(n)
}
#[inline(never)]
fn panics_2(n: i32) -> i32 {
panics_3(n)
}
#[inline(never)]
fn panics_3(n: i32) -> i32 {
panics_4(n)
}
#[inline(never)]
fn panics_4(n: i32) -> i32 {
if n > 0 {
n
} else {
panic!(MyError())
}
}
fn panics(bencher: &mut Bencher) {
bencher.iter(|| {
let _ = panics_impl();
})
}
fn errors_impl() -> i32 {
let mut total = 0;
for n in NUMBERS {
total += errors_1(*n).unwrap_or(0);
}
total
}
#[inline(never)]
fn errors_1(n: i32) -> Result<i32, MyError> {
let v = errors_2(n)?;
Ok(v)
}
#[inline(never)]
fn errors_2(n: i32) -> Result<i32, MyError> {
let v = errors_3(n)?;
Ok(v)
}
#[inline(never)]
fn errors_3(n: i32) -> Result<i32, MyError> {
let v = errors_4(n)?;
Ok(v)
}
#[inline(never)]
fn errors_4(n: i32) -> Result<i32, MyError> {
if n > 0 {
Ok(n)
} else {
Err(MyError())
}
}
fn errors(bencher: &mut Bencher) {
bencher.iter(|| {
let _ = errors_impl();
});
}
benchmark_group!(benches, errors, panics);
benchmark_main!(benches);
running 2 tests
test errors ... bench: 0 ns/iter (+/- 0)
test panics ... bench: 9,430 ns/iter (+/- 48)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured