From c13db788bbd4ee30fed90ae56acf14f7527b2b17 Mon Sep 17 00:00:00 2001 From: "Dmitrij D. Czarkoff" Date: Sat, 13 Aug 2016 23:14:55 +0200 Subject: [PATCH 1/2] Use clock_gettime() instead of clock() clock() provides approximation of processor time used, measured in "clocks". Amount of clock per second varies between 100 (Minix, NetBSD, OpenBSD), 128 (DragonFly BSD, FreeBSD, older versions of MacOS X), 1000 (Windows) and 1000000 (Linux, newer versions of MacOS X, Solaris). The range of these numbers make conversion to milliseconds (the unit of display in `qrtest`) problematic. clock_gettime() with its nanosecond time base appears to be a better and more portable alternative to clock(). --- tests/qrtest.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/tests/qrtest.c b/tests/qrtest.c index e51949b..a3f5c57 100644 --- a/tests/qrtest.c +++ b/tests/qrtest.c @@ -31,7 +31,7 @@ static int want_verbose = 0; static int want_cell_dump = 0; -#define CLOCK_TO_MS(c) ((c) / (CLOCKS_PER_SEC / 1000)) +#define MS(c) ((c) / 1000000) static struct quirc *decoder; @@ -40,9 +40,9 @@ struct result_info { int id_count; int decode_count; - clock_t load_time; - clock_t identify_time; - clock_t total_time; + long load_time; + long identify_time; + long total_time; }; static void print_result(const char *name, struct result_info *info) @@ -58,14 +58,14 @@ static void print_result(const char *name, struct result_info *info) info->id_count); printf("\n"); printf("Total time [load: %ld, identify: %ld, total: %ld]\n", - CLOCK_TO_MS(info->load_time), - CLOCK_TO_MS(info->identify_time), - CLOCK_TO_MS(info->total_time)); + MS(info->load_time), + MS(info->identify_time), + MS(info->total_time)); if (info->file_count) printf("Average time [load: %ld, identify: %ld, total: %ld]\n", - CLOCK_TO_MS(info->load_time / info->file_count), - CLOCK_TO_MS(info->identify_time / info->file_count), - CLOCK_TO_MS(info->total_time / info->file_count)); + MS(info->load_time / info->file_count), + MS(info->identify_time / info->file_count), + MS(info->total_time / info->file_count)); } static void add_result(struct result_info *sum, struct result_info *inf) @@ -85,8 +85,9 @@ static int scan_file(const char *path, const char *filename, int (*loader)(struct quirc *, const char *); int len = strlen(filename); const char *ext; - clock_t start; - clock_t total_start; + struct timespec tp; + long start; + long total_start; int ret; int i; @@ -100,18 +101,22 @@ static int scan_file(const char *path, const char *filename, else return 0; - total_start = start = clock(); + (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); + total_start = start = tp.tv_nsec; ret = loader(decoder, path); - info->load_time = clock() - start; + (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); + info->load_time = tp.tv_nsec - start; if (ret < 0) { fprintf(stderr, "%s: load failed\n", filename); return -1; } - start = clock(); + (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); + start = tp.tv_nsec; quirc_end(decoder); - info->identify_time = clock() - start; + (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); + info->identify_time = tp.tv_nsec - start; info->id_count = quirc_count(decoder); for (i = 0; i < info->id_count; i++) { @@ -124,12 +129,13 @@ static int scan_file(const char *path, const char *filename, info->decode_count++; } - info->total_time += clock() - total_start; + (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); + info->total_time += tp.tv_nsec - total_start; printf(" %-30s: %5ld %5ld %5ld %5d %5d\n", filename, - CLOCK_TO_MS(info->load_time), - CLOCK_TO_MS(info->identify_time), - CLOCK_TO_MS(info->total_time), + MS(info->load_time), + MS(info->identify_time), + MS(info->total_time), info->id_count, info->decode_count); if (want_cell_dump || want_verbose) { From 1de1a466b882011fcb28c29077ce392c4b3b238c Mon Sep 17 00:00:00 2001 From: "Dmitrij D. Czarkoff" Date: Mon, 15 Aug 2016 00:28:24 +0200 Subject: [PATCH 2/2] Store time in milliseconds --- tests/qrtest.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tests/qrtest.c b/tests/qrtest.c index a3f5c57..83827f5 100644 --- a/tests/qrtest.c +++ b/tests/qrtest.c @@ -31,7 +31,7 @@ static int want_verbose = 0; static int want_cell_dump = 0; -#define MS(c) ((c) / 1000000) +#define MS(ts) (unsigned int)((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000)) static struct quirc *decoder; @@ -40,9 +40,9 @@ struct result_info { int id_count; int decode_count; - long load_time; - long identify_time; - long total_time; + unsigned int load_time; + unsigned int identify_time; + unsigned int total_time; }; static void print_result(const char *name, struct result_info *info) @@ -57,15 +57,15 @@ static void print_result(const char *name, struct result_info *info) (info->decode_count * 100 + info->id_count / 2) / info->id_count); printf("\n"); - printf("Total time [load: %ld, identify: %ld, total: %ld]\n", - MS(info->load_time), - MS(info->identify_time), - MS(info->total_time)); + printf("Total time [load: %u, identify: %u, total: %u]\n", + info->load_time, + info->identify_time, + info->total_time); if (info->file_count) - printf("Average time [load: %ld, identify: %ld, total: %ld]\n", - MS(info->load_time / info->file_count), - MS(info->identify_time / info->file_count), - MS(info->total_time / info->file_count)); + printf("Average time [load: %u, identify: %u, total: %u]\n", + info->load_time / info->file_count, + info->identify_time / info->file_count, + info->total_time / info->file_count); } static void add_result(struct result_info *sum, struct result_info *inf) @@ -102,10 +102,10 @@ static int scan_file(const char *path, const char *filename, return 0; (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); - total_start = start = tp.tv_nsec; + total_start = start = MS(tp); ret = loader(decoder, path); (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); - info->load_time = tp.tv_nsec - start; + info->load_time = MS(tp) - start; if (ret < 0) { fprintf(stderr, "%s: load failed\n", filename); @@ -113,10 +113,10 @@ static int scan_file(const char *path, const char *filename, } (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); - start = tp.tv_nsec; + start = MS(tp); quirc_end(decoder); (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); - info->identify_time = tp.tv_nsec - start; + info->identify_time = MS(tp) - start; info->id_count = quirc_count(decoder); for (i = 0; i < info->id_count; i++) { @@ -130,12 +130,12 @@ static int scan_file(const char *path, const char *filename, } (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp); - info->total_time += tp.tv_nsec - total_start; + info->total_time += MS(tp) - total_start; - printf(" %-30s: %5ld %5ld %5ld %5d %5d\n", filename, - MS(info->load_time), - MS(info->identify_time), - MS(info->total_time), + printf(" %-30s: %5u %5u %5u %5d %5d\n", filename, + info->load_time, + info->identify_time, + info->total_time, info->id_count, info->decode_count); if (want_cell_dump || want_verbose) {