I'm relatively new to Rust and have implemented a function to calculate the checksum (SHA-256) of a PDF file. The function works fine for smaller files, but I've noticed a significant increase in execution time for larger files (e.g., 250MB).
Here's the current implementation:
use sha256::try_digest;
use std::id::Error;
fn file_checksum_sha_256(file_path: &str) -> Result<String, Error> {
let file_path = Path::new(file_path);
let file_checksum = try_digest(file_path);
match file_checksum {
Ok(cs) => Ok(cs),
Err(err) => {
eprintln!("Error generating file checksum: {}", err);
Err(err)
}
}
}
When I ran my application with a 250MB PDF file, the execution time was around 13.27 seconds.
time ./MYAPP
real 13.27s
user 13.09s
sys 0.15s
cpu 99%
However, when I used the sha256sum Linux binary on the same file, it took only 1.16 seconds.
time sha256sum MYPDf.pdf
c47d0636584ddc5e28bb55fa9ea7498e58c88fa7af1773df63eb11d54bbe9d43 MYPDF.pdf
real 1.16s
user 1.13s
sys 0.03s
cpu 99%
My questions are:
- Is the performance issue in my Rust code, or is this a normal behavior?
- Could this be related to Rust itself, or is it more about the specific implementation?
- Would introducing multi-threading in my Rust code potentially address this performance gap?