Skip to content

sha2: 0.9.3: Very small effect of asm with SHA-256 / SHA-512? #227

Closed
@35VLG84

Description

@35VLG84

There seem to be very small effect in speed with SHA-256, regardless if asm is activated or not (with sha2:0.9.3 version)

The asm version is only about 10% faster than default / force-soft, and asm version is about 60% of speed Python/OpenSSL implementation.

There is small test case to demonstrate this at the end of issue. However, this also happens while calculating hashes of files, the Python version is about 2x as fast as Rust implementation.

  • cpu: i5-8250U
  • rustc --version: rustc 1.49.0 (e1884a8e3 2020-12-29)
  • rustup toolchain list: stable-x86_64-unknown-linux-gnu (default)

All runs below are done with: cargo clean && cargo build --release && target/release/sha-rs

Default settings: (sha2 = "^0.9.3")

Rust: computing SHA-256 of 1073741824 bytes ('a')
hash: 6ac54ab637d27a69b34ce1ea5c8b98cda7c4b95ec9c0e16143fc5d4343b05113
time: 5.116825 s

Force-soft: (sha2 = { version = "^0.9.3", features = ["force-soft"]})

Rust: computing SHA-256 of 1073741824 bytes ('a')
hash: 6ac54ab637d27a69b34ce1ea5c8b98cda7c4b95ec9c0e16143fc5d4343b05113
time: 5.113324 s

ASM: (sha2 = { version = "^0.9.3", features = ["asm"]})

Rust: computing SHA-256 of 1073741824 bytes ('a')
hash: 6ac54ab637d27a69b34ce1ea5c8b98cda7c4b95ec9c0e16143fc5d4343b05113
time: 4.544902 s

Python (OpenSSL)

Py3: computing SHA-256 of 1073741824 bytes ('a')
hash: 6ac54ab637d27a69b34ce1ea5c8b98cda7c4b95ec9c0e16143fc5d4343b05113
time: 2.569319596 s

Rust example

use sha2::Digest;
use sha2::Sha256;
use std::time::Instant;

fn main() {
    let data_size = 1024;
    let iters = 1024*1024;
    let mut hasher = Sha256::new();
    let data = vec![61;data_size];

    println!("Rust: computing SHA-256 of {} bytes ('a')", data_size*iters);

    let start = Instant::now();
    for _ in 0..iters {
        hasher.update(&data);
    }
    let hash = hasher.finalize();
    let elapsed = Instant::now() - start;

    println!("hash: {:x}", hash);
    println!("time: {} s", elapsed.as_secs_f32());
}

Python example

#!/usr/bin/env python3
import hashlib
import array
import time

if __name__ == '__main__':
    data_size = 1024
    iters = 1024*1024

    hasher = hashlib.sha256()
    data = array.array('B',  [61 for _ in range(0, data_size)])

    print("Py3:  computing SHA-256 of", data_size * iters, "bytes ('a')")

    start = time.process_time()
    for i in range(0, iters):
        hasher.update(data)

    elapsed = time.process_time() - start

    print("hash:", hasher.hexdigest())
    print("time:", elapsed, "s")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions