Last active
February 24, 2024 18:14
-
-
Save allada/6b4321a6487c2888ff73ce1cc0fc86ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use blake3; | |
use sha2; | |
use rand; | |
use sha2::Digest; | |
use rand::RngCore; | |
use std::time::Duration; | |
const RUN_COUNT: usize = 10; | |
const HASH_DATA_SIZE: usize = 1024 * 1024 * 1024; // 1 GB | |
fn main() { | |
let rand_data = { | |
let mut rand_data: Vec<u8> = vec![0; HASH_DATA_SIZE]; | |
rand::thread_rng().fill_bytes(&mut rand_data); | |
rand_data | |
}; | |
let sha256_time = { | |
let mut total = Duration::new(0, 0); | |
for _ in 0..RUN_COUNT { | |
let mut hasher = sha2::Sha256::new(); | |
let start = std::time::Instant::now(); | |
hasher.update(&rand_data); | |
let hash = hasher.finalize(); | |
if hash.as_ptr() == 0 as *const u8 { | |
panic!("This prevents `hash` from being optimized away.") | |
} | |
total += start.elapsed(); | |
} | |
total | |
}; | |
println!("sha256: {:?}", sha256_time); | |
let blake3_time = { | |
let mut total = Duration::new(0, 0); | |
for _ in 0..RUN_COUNT{ | |
let mut hasher = blake3::Hasher::new(); | |
let start = std::time::Instant::now(); | |
hasher.update(&rand_data); | |
let hash = hasher.finalize(); | |
if hash.as_bytes().as_ptr() == 0 as *const u8 { | |
panic!("This prevents `hash` from being optimized away.") | |
} | |
total += start.elapsed(); | |
} | |
total | |
}; | |
println!("blake3: {:?}", blake3_time); | |
println!(""); | |
println!("abs difference: {:?}", sha256_time - blake3_time); | |
println!("% difference r: {:.2}%", (sha256_time.as_nanos() as f64 / blake3_time.as_nanos() as f64) * 100.0); | |
println!("% difference i: {:.2}%", (blake3_time.as_nanos() as f64 / sha256_time.as_nanos() as f64) * 100.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment