Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.62 KB | None | 0 0
  1. #![feature(test)]
  2.  
  3. extern crate test;
  4.  
  5. use test::{black_box, Bencher};
  6. use std::borrow::Cow;
  7.  
  8. const APPEND_LOG: &str = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
  9.  
  10. #[bench]
  11. fn bench_check_no_alloc(b: &mut Bencher) {
  12.     b.iter(|| {
  13.        no_alloc(black_box(APPEND_LOG))
  14.     })
  15. }
  16.  
  17. #[bench]
  18. fn bench_check(b: &mut Bencher) {
  19.     let v = black_box(APPEND_LOG);
  20.     b.iter(|| {
  21.         v.to_lowercase()
  22.     })
  23. }
  24.  
  25. fn no_alloc(s: &str) -> Cow<str> {
  26.    if s.chars().all(char::is_lowercase) {
  27.        Cow::Borrowed(s)
  28.    } else {
  29.        Cow::Owned(s.to_lowercase())
  30.    }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement