Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple bitcoin signature hashing benchmark code
/* Usage: benchmark-sha256 <numtxs> [<numiterations>]
* Benchmark the input signature hashes for a maximum-sized transaction with this many
* inputs
*
* Rusty Russell, <rusty@rustcorp.com.au> GPLv3.
*/
#include <ccan/crypto/sha256/sha256.c>
#include <ccan/time/time.h>
#include <stdio.h>
#include <string.h>
//#define BLOCKSIZE 1000000
#define BLOCKSIZE 8000000
int main(int argc, char *argv[])
{
struct timeabs start;
struct timerel diff;
size_t i, n, num_inputs = atoi(argv[1]);
union {
struct sha256 h;
char rest[BLOCKSIZE - 180 * num_inputs];
} block;
n = atoi(argv[2] ? argv[2] : "10000");
memset(&block, 0, sizeof(block));
sha256(&block.h, &n, sizeof(n));
start = time_now();
for (i = 0; i < n; i++) {
sha256(&block.h, &block, sizeof(block));
sha256(&block.h, &block.h, sizeof(block.h));
}
diff = time_divide(time_between(time_now(), start), n);
printf("%zu:%zu:%llu:%02x%02x%02x%02x%02x%02x...\n",
num_inputs, sizeof(block),
(unsigned long long)time_to_usec(time_multiply(diff, num_inputs)),
block.h.u.u8[0], block.h.u.u8[1], block.h.u.u8[2],
block.h.u.u8[3], block.h.u.u8[4], block.h.u.u8[5]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment