6 static inline void compute_hash(const struct git_hash_algo *algo, git_hash_ctx *ctx, uint8_t *final, const void *p, size_t len)
9 algo->update_fn(ctx, p, len);
10 algo->final_fn(final, ctx);
13 int cmd__hash_speed(int ac, const char **av)
16 unsigned char hash[GIT_MAX_RAWSZ];
17 clock_t initial, start, end;
18 unsigned bufsizes[] = { 64, 256, 1024, 8192, 16384 };
21 const struct git_hash_algo *algo = NULL;
24 for (i = 1; i < GIT_HASH_NALGOS; i++) {
25 if (!strcmp(av[1], hash_algos[i].name)) {
26 algo = &hash_algos[i];
32 die("usage: test-tool hash-speed algo_name");
34 /* Use this as an offset to make overflow less likely. */
37 printf("algo: %s\n", algo->name);
39 for (i = 0; i < ARRAY_SIZE(bufsizes); i++) {
42 p = xcalloc(1, bufsizes[i]);
43 start = end = clock() - initial;
44 for (j = 0; ((end - start) / CLOCKS_PER_SEC) < NUM_SECONDS; j++) {
45 compute_hash(algo, &ctx, hash, p, bufsizes[i]);
48 * Only check elapsed time every 128 iterations to avoid
49 * dominating the runtime with system calls.
52 end = clock() - initial;
55 kb_per_sec = kb / (1024 * ((double)end - start) / CLOCKS_PER_SEC);
56 printf("size %u: %lu iters; %lu KiB; %0.2f KiB/s\n", bufsizes[i], j, kb, kb_per_sec);