Skip to content

Instantly share code, notes, and snippets.

@yytyd
Created September 12, 2020 15:01
Show Gist options
  • Save yytyd/e0858e4b6c56b16535f0260897dcdf27 to your computer and use it in GitHub Desktop.
Save yytyd/e0858e4b6c56b16535f0260897dcdf27 to your computer and use it in GitHub Desktop.
f64 の to_string のベンチマーク(通常の to_string vs Ryu を使った to_string)
#![feature(test)]
fn main() {
println!("Hello, world!");
}
fn from_f64_to_string(value: f64) -> String {
value.to_string()
}
fn from_f64_to_string_by_ryu(value: f64) -> String {
let mut buffer = ryu::Buffer::new();
let printed = buffer.format(value);
printed.to_string()
}
fn from_i64_to_string(value: i64) -> String {
value.to_string()
}
#[cfg(test)]
mod tests {
extern crate test;
use super::*;
use test::Bencher;
// 適切に比較できているのかは自信ない…
#[bench]
fn bench_f64_to_string(b: &mut Bencher) {
b.iter(|| from_f64_to_string(1.23));
}
#[bench]
fn bench_f64_to_string_by_ryu(b: &mut Bencher) {
b.iter(|| from_f64_to_string_by_ryu(1.23));
}
#[bench]
fn bench_i64_to_string(b: &mut Bencher) {
b.iter(|| from_i64_to_string(123));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment