Skip to content

Instantly share code, notes, and snippets.

@thomcc
Last active May 5, 2021 21:47
Show Gist options
  • Save thomcc/d017dec2bf7fbfd017e4f34cfd4db6f8 to your computer and use it in GitHub Desktop.
Save thomcc/d017dec2bf7fbfd017e4f34cfd4db6f8 to your computer and use it in GitHub Desktop.
// Note: probably not a good idea to check in directly,
// since it will cause this benchmark to fluctuate when
// ext_slice.rs changes size...
const SOURCE: &str = include_str!("../../src/ext_slice.rs");
fn source_lines() -> Vec<String> {
SOURCE.lines().map(|s| format!("{}\r\n", s)).collect::<Vec<_>>()
}
define(c, "bstr/trim", "source-lines", SOURCE.as_bytes(), move |b| {
let lines = source_lines();
b.iter(|| {
for line in &lines {
criterion::black_box(line.as_bytes().trim());
}
});
});
define(c, "std/trim", "source-lines", SOURCE.as_bytes(), move |b| {
let lines = source_lines();
b.iter(|| {
for line in &lines {
criterion::black_box(line.trim());
}
});
});
let large_ascii_padded = || format!("{0}x{0}", " \t\r\n".repeat(512));
define(
c,
"bstr/trim",
"large-ascii-padded",
large_ascii_padded().as_bytes(),
move |b| {
let corpus = large_ascii_padded();
b.iter(|| {
assert_eq!("x".as_bytes(), corpus.as_bytes().trim());
});
},
);
define(
c,
"std/trim",
"large-ascii-padded",
large_ascii_padded().as_bytes(),
move |b| {
let corpus = large_ascii_padded();
b.iter(|| {
assert_eq!("x", corpus.trim());
});
},
);
diff --git a/bench/src/bench.rs b/bench/src/bench.rs
index 3fa5ae0..269e33c 100644
--- a/bench/src/bench.rs
+++ b/bench/src/bench.rs
@@ -132,6 +132,55 @@ fn trim(c: &mut Criterion) {
assert_eq!("foo\tbar", corpus.trim());
});
});
+
+ // Note: probably not a good idea to check in directly,
+ // since it will cause this benchmark to fluctuate when
+ // ext_slice.rs changes size...
+ const SOURCE: &str = include_str!("../../src/ext_slice.rs");
+ fn source_lines() -> Vec<String> {
+ SOURCE.lines().map(|s| format!("{}\r\n", s)).collect::<Vec<_>>()
+ }
+ define(c, "bstr/trim", "source-lines", SOURCE.as_bytes(), move |b| {
+ let lines = source_lines();
+ b.iter(|| {
+ for line in &lines {
+ criterion::black_box(line.as_bytes().trim());
+ }
+ });
+ });
+ define(c, "std/trim", "source-lines", SOURCE.as_bytes(), move |b| {
+ let lines = source_lines();
+ b.iter(|| {
+ for line in &lines {
+ criterion::black_box(line.trim());
+ }
+ });
+ });
+ let large_ascii_padded = || format!("{0}x{0}", " \t\r\n".repeat(512));
+ define(
+ c,
+ "bstr/trim",
+ "large-ascii-padded",
+ large_ascii_padded().as_bytes(),
+ move |b| {
+ let corpus = large_ascii_padded();
+ b.iter(|| {
+ assert_eq!("x".as_bytes(), corpus.as_bytes().trim());
+ });
+ },
+ );
+ define(
+ c,
+ "std/trim",
+ "large-ascii-padded",
+ large_ascii_padded().as_bytes(),
+ move |b| {
+ let corpus = large_ascii_padded();
+ b.iter(|| {
+ assert_eq!("x", corpus.trim());
+ });
+ },
+ );
}
fn chars(c: &mut Criterion) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment