Skip to content

Instantly share code, notes, and snippets.

@mehcode
Created June 26, 2018 05:17
Show Gist options
  • Save mehcode/79e718a07acb80a88c69961b9a30aa47 to your computer and use it in GitHub Desktop.
Save mehcode/79e718a07acb80a88c69961b9a30aa47 to your computer and use it in GitHub Desktop.
Benchmark sha1 implementations
extern crate sha1;
// extern crate sha1 as sha_1;
extern crate ring;
use std::io::{Read, Write};
use std::time::{Instant, Duration};
use std::process::{Command, Stdio};
fn time<F, FMT>(desc: &str, f: F, fmt: FMT) where F: Fn(), FMT: Fn(Duration) -> String {
let start = Instant::now();
f();
let duration = Instant::now() - start;
println!("{}: {}", desc, fmt(duration));
}
fn main() {
let mut out = Vec::<u8>::new();
std::io::stdin().read_to_end(&mut out).unwrap();
let throughput = |duration: Duration| {
let s = duration.as_secs() as f64;
let ns = duration.subsec_nanos() as f64 / 1000000000.0;
format!("{:.2} MB/s", out.len() as f64 / (s + ns) / 1000000.0)
};
time("sha1sum program", || {
let mut child = Command::new("sha1sum")
.stdin(Stdio::piped())
.spawn().unwrap();
if let Some(ref mut stdin) = child.stdin {
stdin.write(&out).unwrap();
}
child.wait().unwrap();
}, &throughput);
time("sha1 crate", || {
let mut sha1 = sha1::Sha1::new();
sha1.update(&out);
let _ = sha1.digest();
}, &throughput);
// time("sha-1 crate", || {
// use sha_1::Digest;
// let mut sha1 = sha_1::Sha1::default();
// sha1.input(&out);
// // println!("{:?}", sha1.result().as_slice().to_hex());
// let _ = sha1.result();
// }, &throughput);
time("ring crate", || {
let _ = ring::digest::digest(&ring::digest::SHA1, &out);
}, &throughput);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment