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().
This commit is contained in:
Dmitrij D. Czarkoff 2016-08-13 23:14:55 +02:00
parent e9b279786a
commit c13db788bb

View file

@ -31,7 +31,7 @@
static int want_verbose = 0; static int want_verbose = 0;
static int want_cell_dump = 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; static struct quirc *decoder;
@ -40,9 +40,9 @@ struct result_info {
int id_count; int id_count;
int decode_count; int decode_count;
clock_t load_time; long load_time;
clock_t identify_time; long identify_time;
clock_t total_time; long total_time;
}; };
static void print_result(const char *name, struct result_info *info) 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); info->id_count);
printf("\n"); printf("\n");
printf("Total time [load: %ld, identify: %ld, total: %ld]\n", printf("Total time [load: %ld, identify: %ld, total: %ld]\n",
CLOCK_TO_MS(info->load_time), MS(info->load_time),
CLOCK_TO_MS(info->identify_time), MS(info->identify_time),
CLOCK_TO_MS(info->total_time)); MS(info->total_time));
if (info->file_count) if (info->file_count)
printf("Average time [load: %ld, identify: %ld, total: %ld]\n", printf("Average time [load: %ld, identify: %ld, total: %ld]\n",
CLOCK_TO_MS(info->load_time / info->file_count), MS(info->load_time / info->file_count),
CLOCK_TO_MS(info->identify_time / info->file_count), MS(info->identify_time / info->file_count),
CLOCK_TO_MS(info->total_time / info->file_count)); MS(info->total_time / info->file_count));
} }
static void add_result(struct result_info *sum, struct result_info *inf) 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 (*loader)(struct quirc *, const char *);
int len = strlen(filename); int len = strlen(filename);
const char *ext; const char *ext;
clock_t start; struct timespec tp;
clock_t total_start; long start;
long total_start;
int ret; int ret;
int i; int i;
@ -100,18 +101,22 @@ static int scan_file(const char *path, const char *filename,
else else
return 0; return 0;
total_start = start = clock(); (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
total_start = start = tp.tv_nsec;
ret = loader(decoder, path); 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) { if (ret < 0) {
fprintf(stderr, "%s: load failed\n", filename); fprintf(stderr, "%s: load failed\n", filename);
return -1; return -1;
} }
start = clock(); (void)clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
start = tp.tv_nsec;
quirc_end(decoder); 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); info->id_count = quirc_count(decoder);
for (i = 0; i < info->id_count; i++) { 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->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, printf(" %-30s: %5ld %5ld %5ld %5d %5d\n", filename,
CLOCK_TO_MS(info->load_time), MS(info->load_time),
CLOCK_TO_MS(info->identify_time), MS(info->identify_time),
CLOCK_TO_MS(info->total_time), MS(info->total_time),
info->id_count, info->decode_count); info->id_count, info->decode_count);
if (want_cell_dump || want_verbose) { if (want_cell_dump || want_verbose) {