Feat: run multiple benchmarking cycles
This commit is contained in:
parent
61be4c0382
commit
44bff07e02
3 changed files with 18 additions and 11 deletions
|
@ -38,13 +38,14 @@ enum argumentParseResult parseArguments(int argc, char** argv,
|
||||||
/**
|
/**
|
||||||
* @brief Run an md2_hash_func with benchmark timing
|
* @brief Run an md2_hash_func with benchmark timing
|
||||||
*
|
*
|
||||||
|
* @param cycles the number of cycles to execute
|
||||||
* @param func the function to run
|
* @param func the function to run
|
||||||
* @param len the lenght of buf
|
* @param len the lenght of buf
|
||||||
* @param buf the data to hash
|
* @param buf the data to hash
|
||||||
* @param out the target array for the hash
|
* @param out the target array for the hash
|
||||||
* @return double
|
* @return double
|
||||||
*/
|
*/
|
||||||
double run_benchmark(md2_hash_func func, size_t len, uint8_t* buf,
|
double run_benchmark(unsigned cycles, md2_hash_func func, size_t len,
|
||||||
uint8_t* out);
|
uint8_t* buf, uint8_t* out);
|
||||||
|
|
||||||
#endif // HELPER_H
|
#endif // HELPER_H
|
|
@ -93,10 +93,12 @@ double current_time(void) {
|
||||||
return t.tv_sec + t.tv_nsec * 1e-9;
|
return t.tv_sec + t.tv_nsec * 1e-9;
|
||||||
}
|
}
|
||||||
|
|
||||||
double run_benchmark(md2_hash_func func, size_t len, uint8_t *buf,
|
double run_benchmark(unsigned cycles, md2_hash_func func, size_t len,
|
||||||
uint8_t *out) {
|
uint8_t *buf, uint8_t *out) {
|
||||||
double start = current_time();
|
double start = current_time();
|
||||||
func(len, buf, out);
|
for (; cycles > 0; cycles--) {
|
||||||
|
func(len, buf, out);
|
||||||
|
}
|
||||||
double end = current_time();
|
double end = current_time();
|
||||||
|
|
||||||
return end - start;
|
return end - start;
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
static bool runTest(const char* message, const char* expectedHash) {
|
static bool runTest(const char* message, const char* expectedHash) {
|
||||||
uint8_t out[16];
|
uint8_t out[16];
|
||||||
double duration =
|
double duration =
|
||||||
run_benchmark(md2_hash, strlen(message), (uint8_t*)message, out);
|
run_benchmark(1, md2_hash, strlen(message), (uint8_t*)message, out);
|
||||||
|
|
||||||
char hash[32];
|
char hash[32];
|
||||||
md2_encode_hash(out, hash);
|
md2_encode_hash(out, hash);
|
||||||
|
@ -86,13 +86,17 @@ int main(int argc, char** argv) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
uint8_t out[16];
|
uint8_t out[16];
|
||||||
double duration = run_benchmark(md2_hash, len, data, out);
|
|
||||||
|
|
||||||
printf("Hash: ");
|
|
||||||
char hash[32];
|
char hash[32];
|
||||||
|
if (c.doBenchmark) {
|
||||||
|
double duration =
|
||||||
|
run_benchmark(c.benchmarkingCycles, md2_hash, len, data, out);
|
||||||
|
printf("Running %d cycles took %f seconds\n", c.benchmarkingCycles,
|
||||||
|
duration);
|
||||||
|
} else {
|
||||||
|
md2_hash(len, data, out);
|
||||||
|
}
|
||||||
md2_encode_hash(out, hash);
|
md2_encode_hash(out, hash);
|
||||||
|
printf("Hash: %s\n", hash);
|
||||||
printf("%s, took: %f\n", hash, duration);
|
|
||||||
|
|
||||||
free(data);
|
free(data);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue