Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: shalldonow on October 25, 2023, 06:30:20 PM



Title: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: shalldonow on October 25, 2023, 06:30:20 PM
Is there a reliable source of information, maybe academic work or independent analysis with statistics, on the average number of sha256 hashes a consumer-grade CPU can compute?

Otherwise, could somebody help me write a C code or Rust code with threads to run the computations in a multicore computer? So I can test it locally.

I just would like to know a rough estimate. I'd like to know the order of magnitude, such as hundreds of thousands or maybe millions of hashes per second.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: Gabrics on October 25, 2023, 06:58:44 PM
https://stackoverflow.com/questions/4764026/how-many-sha256-hashes-can-a-modern-computer-compute

There is a code you can run and post the results.

I believe though that this is not relevant as GPUs are WAAAAY better. So pointless to run this on CPU, look for CUDA implementation.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: digaran on October 25, 2023, 07:08:03 PM
Maybe this topic (https://bitcointalk.org/index.php?topic=5447485.msg62031050#msg62031050) could also help you. The link on stackoverflow site is for 2012.

Edit: I remember a powerful tool capable of running different hash functions and doing benchmark. It's called hashcat, from hashcat.net😉


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: seek3r on October 25, 2023, 07:20:57 PM

Otherwise, could somebody help me write a C code or Rust code with threads to run the computations in a multicore computer? So I can test it locally.


Might could help you with that.

Can you give me infos about ur CPU that will be used for this test? Need to know so I can set the maximum of threads that will be used.
Otherwise I will just set two variables so you can adjust it while testing. One for the number of used threads and one for the amount of hashes per thread.
Rust is fine for you?


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: seoincorporation on October 25, 2023, 10:47:00 PM
I just made a script for this, that way op can verify how fast his PC can generate sha codes.

Code:
time for a in $(seq 1 10); do echo "$a" | sha256sum; done

The code is for linux and what it does is to generate 10 sha256 and print the time:

Quote
real   0m0.019s
user   0m0.015s
sys   0m0.008s

Now let's try with 1000 and see the time.

Quote
real   0m1.501s
user   0m1.413s
sys   0m0.570s

And with 10,000

Quote
real   0m16.384s
user   0m14.474s
sys   0m5.943s

And i get these results with this CPU, maybe other users could test and post their results with a better PC:

Code:
*-cpu
          product: Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz
          vendor: Intel Corp.
          physical id: 1
          bus info: cpu@0
          version: 6.94.3
          size: 2660MHz
          capacity: 3200MHz
          width: 64 bits


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: shalldonow on October 25, 2023, 11:49:47 PM

Otherwise, could somebody help me write a C code or Rust code with threads to run the computations in a multicore computer? So I can test it locally.


Might could help you with that.

Can you give me infos about ur CPU that will be used for this test? Need to know so I can set the maximum of threads that will be used.
Otherwise I will just set two variables so you can adjust it while testing. One for the number of used threads and one for the amount of hashes per thread.
Rust is fine for you?

I have a linux machine, x86_64 architecture, 4-core Intel. Thank you!


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: shalldonow on October 26, 2023, 12:11:21 AM
I just made a script for this, that way op can verify how fast his PC can generate sha codes.

Code:
time for a in $(seq 1 10); do echo "$a" | sha256sum; done

The code is for linux and what it does is to generate 10 sha256 and print the time:

Quote
real   0m0.019s
user   0m0.015s
sys   0m0.008s

Now let's try with 1000 and see the time.

Quote
real   0m1.501s
user   0m1.413s
sys   0m0.570s

And with 10,000

Quote
real   0m16.384s
user   0m14.474s
sys   0m5.943s

And i get these results with this CPU, maybe other users could test and post their results with a better PC:

Code:
*-cpu
          product: Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz
          vendor: Intel Corp.
          physical id: 1
          bus info: cpu@0
          version: 6.94.3
          size: 2660MHz
          capacity: 3200MHz
          width: 64 bits

Thanks for the code!

However, it understimates the computations because it runs shell commands each time. For every hash, the operating system will load the SHA256SUM binary into memory, make the proper system calls, then execute. There is a lot of CPU cycles there which are not used for computing the hashes.

I think it the proper way is to compute millions of hashes within a C or Rust program (maybe C++ too) and also measure the time within the program itself, using the languages libraries.

That's because running the `time` command line ulitiy also takes into account the time to load the program into memory, setup the main function, exit the program, which is a lot of system calls and numerous CPU cycles that are not used for the computation of the hashes and they do take some miliseconds.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: pooya87 on October 26, 2023, 04:25:28 AM
It depends on the message you want to compute the hash for.
The smaller the size of the message the lower the number of blocks (internally used in SHA256) hence the faster it will compute. For example computing hash for 10 bytes is faster than 33 bytes and faster than 80 bytes and faster than 200 bytes.
It is slower if you want to compute double SHA256 hash.

In the context of bitcoin we almost always compute double SHA256.
33 bytes is size of majority of public keys that are hashed in the most common OP_HASH160 scripts.
80 byte is the header size that is hashed in mining and you can easily find stats for both CPUs and GPUs hashrates on the internet like [1].

[1] https://en.bitcoin.it/wiki/Non-specialized_hardware_comparison


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: seek3r on October 26, 2023, 06:10:11 AM
I have a linux machine, x86_64 architecture, 4-core Intel. Thank you!

Alright thanks. Tried something on Rust.
You can test around with the NUM_THREADS - set it to 8 if your cpu supports hyper-threading. You can test around and increase NUM_HASHES by every round to get a more precise result.

Since I used crypto and rayon crates you have to add them as dependencies on your cargo.toml with their latest versions:

Code:
[dependencies]
crypto = "0.2.36"
rayon = "1.8.0"

After that, implement this code:

Code:
extern crate crypto;
extern crate rayon;

use crypto::digest::Digest;
use crypto::sha2::Sha256;
use rayon::prelude::*;
use std::time::Instant;

const NUM_HASHES: usize = 1000000;
const NUM_THREADS: usize = 4;

fn compute_hashes() {
    let string = "sha256_maxhash".as_bytes();
    let mut sha = Sha256::new();
    for _ in 0..NUM_HASHES {
        sha.input(string);
        let _ = sha.result_str();
        sha.reset();
    }
}

fn main() {
    rayon::ThreadPoolBuilder::new()
        .num_threads(NUM_THREADS)
        .build_global()
        .unwrap();

    let start = Instant::now();
    (0..NUM_THREADS).into_par_iter().for_each(|_| compute_hashes());
    let duration = start.elapsed();

    let total_hashes = NUM_HASHES * NUM_THREADS;
    let hashes_per_second = total_hashes as f64 / duration.as_secs_f64();
    println!("Time taken: {:?} seconds", duration);
    println!("Hash rate: {:.0} hashes per second", hashes_per_second);
}

Save it and you are done! To run it just execute it via cargo run --release in the same directory.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: NotATether on October 26, 2023, 08:41:54 AM
As far as SHA256 performance on CPU is concerned, there are special instructions (https://en.wikipedia.org/wiki/Intel_SHA_extensions) available on most CPUs for accelerating the computation of SHA256 hashes.

These are available per-thread, so the performance you get from using some generic SHA-256 function is going to be slower than a specialized function that makes use of these opcodes.

Having said that, someone made a performance benchmark to check how fast calculating SHA256 your CPU is, inside the browser: https://www.measurethat.net/Benchmarks/Show/1246/0/sha256


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: DaveF on October 26, 2023, 11:22:33 AM
As far as SHA256 performance on CPU is concerned, there are special instructions (https://en.wikipedia.org/wiki/Intel_SHA_extensions) available on most CPUs for accelerating the computation of SHA256 hashes.

These are available per-thread, so the performance you get from using some generic SHA-256 function is going to be slower than a specialized function that makes use of these opcodes.

Having said that, someone made a performance benchmark to check how fast calculating SHA256 your CPU is, inside the browser: https://www.measurethat.net/Benchmarks/Show/1246/0/sha256

Not even tl;dr, just dr but isn't that going to only be able to use 1 core of a CPU? Usually most browsers will limit that.

But no matter what running it in a browser although simple, will never give good results due to overhead.

-Dave


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: ABCbits on October 26, 2023, 11:54:18 AM
While some member provide nice answer, i would just recommend OP to search for result of Hashcat benchmark result for certain device. Few example,
1. Apple M1 Ultra, 1785.4MH/s. Source, https://gist.github.com/Chick3nman/ccfb883d2d267d94770869b09f5b96ed (https://gist.github.com/Chick3nman/ccfb883d2d267d94770869b09f5b96ed).
2. 8x Nvidia GTX 1080, 23012.1 MH/s. Source, https://gist.github.com/epixoip/a83d38f412b4737e99bbef804a270c40 (https://gist.github.com/epixoip/a83d38f412b4737e99bbef804a270c40).

Having said that, someone made a performance benchmark to check how fast calculating SHA256 your CPU is, inside the browser: https://www.measurethat.net/Benchmarks/Show/1246/0/sha256

Doesn't seem reliable to me since different browser and browser version could affect benchmark result.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: shalldonow on October 26, 2023, 01:46:08 PM
I have a linux machine, x86_64 architecture, 4-core Intel. Thank you!

Alright thanks. Tried something on Rust.
You can test around with the NUM_THREADS - set it to 8 if your cpu supports hyper-threading. You can test around and increase NUM_HASHES by every round to get a more precise result.

Since I used crypto and rayon crates you have to add them as dependencies on your cargo.toml with their latest versions:

Code:
[dependencies]
crypto = "0.2.36"
rayon = "1.8.0"

After that, implement this code:

Code:
extern crate crypto;
extern crate rayon;

use crypto::digest::Digest;
use crypto::sha2::Sha256;
use rayon::prelude::*;
use std::time::Instant;

const NUM_HASHES: usize = 1000000;
const NUM_THREADS: usize = 4;

fn compute_hashes() {
    let string = "sha256_maxhash".as_bytes();
    let mut sha = Sha256::new();
    for _ in 0..NUM_HASHES {
        sha.input(string);
        let _ = sha.result_str();
        sha.reset();
    }
}

fn main() {
    rayon::ThreadPoolBuilder::new()
        .num_threads(NUM_THREADS)
        .build_global()
        .unwrap();

    let start = Instant::now();
    (0..NUM_THREADS).into_par_iter().for_each(|_| compute_hashes());
    let duration = start.elapsed();

    let total_hashes = NUM_HASHES * NUM_THREADS;
    let hashes_per_second = total_hashes as f64 / duration.as_secs_f64();
    println!("Time taken: {:?} seconds", duration);
    println!("Hash rate: {:.0} hashes per second", hashes_per_second);
}

Save it and you are done! To run it just execute it via cargo run --release in the same directory.


Thank you very much! It worked for my purposes!


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: seek3r on October 26, 2023, 01:49:31 PM
Thank you very much! It worked for my purposes!
Glad to hear that!
Always a pleasure, especially if it means I can improve/check my Rust skills.    8)


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: shalldonow on October 26, 2023, 01:53:22 PM
Using the code provided by seek3r, with some adaptions (such as using more recent package versions for the sha2 and digests crates) I was able to obtain around over 3 million double sha256 hashes per second with 2 threads. I actually computed double sha256 hashes (the output of the first digest being the input for the second), so it would be 6 million single hashes. The tests were done in a x86_64 architecture machine, running 2 threads, and each thread ran on a Intel CPU with 2.60 GHz (upper bound 4GHz).


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: NotFuzzyWarm on October 28, 2023, 12:48:15 AM
Keep in mind that Intel, AMD,et al have had support for hardware sha256 functions (https://en.wikipedia.org/wiki/Intel_SHA_extensions) since at least 2013 and the more recent generations of Intel & AMD cpu's now have said hardware built into the chip. Sorta like the early PC days when there were 1st math coprocessors for the 186, 286 & 386 cpu's followed by the 486 with the coprocessor in the CPU chip. Details of the Intel extensions is here. (https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sha-extensions.html)

Thing is that they are for full data encryption & decryption functions both of which rely on knowing both the private and public keys for speedy processing - not mining where we only try to randomly find the right key.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: odolvlobo on October 29, 2023, 06:03:55 PM
A long time ago, the Bitcoin mining hash rate on a CPU was up to 3 MH/s, and up to 1 GH/s on a GPU. They may be somewhat higher now, but still the same order of magnitude.

Here is a chart: https://en.bitcoin.it/wiki/Non-specialized_hardware_comparison


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: ymgve2 on October 30, 2023, 08:39:10 PM
This seems to be an example of the XY problem

https://en.wikipedia.org/wiki/XY_problem

It would be easier if you told us why you need the number of hashes a consumer grade CPU can do, so we can better aid with the underlying problem you've encountered.


Title: Re: how many sha256 hashes can a consumer-grade CPU compute per second in average?
Post by: philipma1957 on November 07, 2023, 01:17:54 PM
A long time ago, the Bitcoin mining hash rate on a CPU was up to 3 MH/s, and up to 1 GH/s on a GPU. They may be somewhat higher now, but still the same order of magnitude.

Here is a chart: https://en.bitcoin.it/wiki/Non-specialized_hardware_comparison


Core i5 2500K   4/4   20.6mh from your chart is accurate as I used to mine just a tiny bit with an i5 2500  on the pool bitminter back in 2012