Skip to content

Instantly share code, notes, and snippets.

@zohnannor
Created March 18, 2022 14:16
Show Gist options
  • Save zohnannor/93255b9fc505cd4b6841b52c4f92fcbc to your computer and use it in GitHub Desktop.
Save zohnannor/93255b9fc505cd4b6841b52c4f92fcbc to your computer and use it in GitHub Desktop.
what 0_o
#![feature(test)]
#[cfg(test)]
mod test {
extern crate test;
use std::{hint, ops::RangeInclusive};
use rand::{distributions::Uniform, Rng};
use test::Bencher;
fn generate_string(char_range: RangeInclusive<char>) -> String {
rand::thread_rng()
.sample_iter(&Uniform::from(char_range))
.take(100_000)
.collect()
}
mod ascii {
use super::*;
fn get_text() -> String {
generate_string(' '..='~')
}
#[bench]
fn time_black_box_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for x in text.bytes() {
test::black_box(x);
}
});
}
#[bench]
fn time_black_box_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for x in text.chars() {
test::black_box(x);
}
});
}
#[bench]
fn time_hint_loop_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for _ in text.bytes() {
hint::spin_loop();
}
});
}
#[bench]
fn time_hint_loop_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for _ in text.chars() {
hint::spin_loop();
}
});
}
#[bench]
fn time_vec_store_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
let mut v = Vec::with_capacity(text.len());
for x in text.bytes() {
v.push(x);
}
assert!(v.len() == v.capacity());
});
}
#[bench]
fn time_vec_store_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
let mut v = Vec::with_capacity(text.len() / 4);
for x in text.chars() {
v.push(x);
}
assert!(v.len() == v.capacity());
});
}
}
mod non_ascii {
use super::*;
fn get_text() -> String {
generate_string('Я'..='а')
}
#[bench]
fn time_black_box_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for x in text.bytes() {
test::black_box(x);
}
});
}
#[bench]
fn time_black_box_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for x in text.chars() {
test::black_box(x);
}
});
}
#[bench]
fn time_hint_loop_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for _ in text.bytes() {
hint::spin_loop();
}
});
}
#[bench]
fn time_hint_loop_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for _ in text.chars() {
hint::spin_loop();
}
});
}
#[bench]
fn time_vec_store_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
let mut v = Vec::with_capacity(text.len());
for x in text.bytes() {
v.push(x);
}
assert!(v.len() == v.capacity());
});
}
#[bench]
fn time_vec_store_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
let mut v = Vec::with_capacity(text.len() / 4);
for x in text.chars() {
v.push(x);
}
assert!(v.len() == v.capacity());
});
}
}
mod emoji {
use super::*;
fn get_text() -> String {
generate_string('💅'..='😀')
}
#[bench]
fn time_black_box_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for x in text.bytes() {
test::black_box(x);
}
});
}
#[bench]
fn time_black_box_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for x in text.chars() {
test::black_box(x);
}
});
}
#[bench]
fn time_hint_loop_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for _ in text.bytes() {
hint::spin_loop();
}
});
}
#[bench]
fn time_hint_loop_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
for _ in text.chars() {
hint::spin_loop();
}
});
}
#[bench]
fn time_vec_store_bytes(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
let mut v = Vec::with_capacity(text.len());
for x in text.bytes() {
v.push(x);
}
assert!(v.len() == v.capacity());
});
}
#[bench]
fn time_vec_store_chars(b: &mut Bencher) {
let text = get_text();
b.iter(|| {
let mut v = Vec::with_capacity(text.len() / 4);
for x in text.chars() {
v.push(x);
}
assert!(v.len() == v.capacity());
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment