Last active
August 13, 2019 08:09
-
-
Save ubnt-intrepid/b480afa82b5ae9f4a200564c9850682a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(test)] | |
| extern crate test; | |
| use test::Bencher; | |
| use test::black_box; | |
| use std::panic::catch_unwind; | |
| fn foo_expect(n: i32) { | |
| let a = bar(n).expect(&format!("n = {}", n)); | |
| println!("{}", a); | |
| } | |
| fn foo_if_let(n: i32) { | |
| let a = if let Some(a) = bar(n) { | |
| a | |
| } else { | |
| panic!("n = {}", n); | |
| }; | |
| println!("{}", a); | |
| } | |
| fn foo_if_let_format(n: i32) { | |
| let a = if let Some(a) = bar(n) { | |
| a | |
| } else { | |
| panic!("{}", format!("n = {}", n)); | |
| }; | |
| println!("{}", a); | |
| } | |
| fn foo_unwrap_or_else(n: i32) { | |
| let a = bar(n).unwrap_or_else(|| panic!("n = {}", n)); | |
| println!("{}", a); | |
| } | |
| fn bar(n: i32) -> Option<i32> { | |
| if n == 0 { | |
| None | |
| } else { | |
| Some(1000 / n) | |
| } | |
| } | |
| #[bench] | |
| fn bench_expect(b: &mut Bencher) { | |
| let n = black_box(0); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_expect(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_expect_nonzero(b: &mut Bencher) { | |
| let n = black_box(1); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_expect(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_if_let(b: &mut Bencher) { | |
| let n = black_box(0); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_if_let(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_if_let_nonzero(b: &mut Bencher) { | |
| let n = black_box(1); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_if_let(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_if_let_format(b: &mut Bencher) { | |
| let n = black_box(0); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_if_let_format(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_if_let_format_nonzero(b: &mut Bencher) { | |
| let n = black_box(1); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_if_let_format(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_unwrap_or_else(b: &mut Bencher) { | |
| let n = black_box(0); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_unwrap_or_else(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn bench_unwrap_or_else_nonzero(b: &mut Bencher) { | |
| let n = black_box(1); | |
| b.iter(|| { | |
| let _ = catch_unwind(|| foo_unwrap_or_else(n)); | |
| }); | |
| } | |
| #[bench] | |
| fn beseline(b: &mut Bencher) { | |
| b.iter(|| { | |
| let _ = catch_unwind(|| panic!("n = {}", 0)); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.