void calc_gradients_whole_pic(int *gradient, uint8_t *image, int width, int height) { assert(gradient != NULL); assert(image != NULL); clock_t start; #define BENCH_INIT(repetitions) \ start = clock(); \ for (int i = 0; i < repetitions; i++) { #define BENCH_FINISH(method) \ } \ printf("\n[%s] elapsed time: %.4f\n", method, \ (double)(clock() - start) / CLOCKS_PER_SEC); int repetitions = 50; printf("benchmarking ... repetitions %d\n", repetitions); BENCH_INIT(repetitions); _calc_gradients_pix_by_pix(gradient, image, width, height); BENCH_FINISH("OnePixelEachLoop"); BENCH_INIT(repetitions); _calc_gradients_line_by_line(gradient, image, width, height); BENCH_FINISH("OneLineEachLoop"); BENCH_INIT(repetitions); _calc_gradients_by_mutiple_lines(gradient, image, width, height); BENCH_FINISH("MultipleLinesEachLoop"); #undef BENCH_INIT #undef BENCH_FINISH /* printf("\nGradient Image:\n"); */ /* int i, j; */ /* for (j = 0; j < height; j++) { */ /* for (i = 0; i < width; i++) { */ /* printf("%3d ", gradient[j * width + i]); */ /* } */ /* printf("\n"); */ /* } */ }
int main (int argc, char **argv) { unsigned long i, k; struct timespec runtime; timing_t start, end; bool detailed = false; json_ctx_t json_ctx; if (argc == 2 && !strcmp (argv[1], "-d")) detailed = true; bench_start (); memset (&runtime, 0, sizeof (runtime)); unsigned long iters, res; #ifdef BENCH_INIT BENCH_INIT (); #endif TIMING_INIT (res); iters = 1000 * res; json_init (&json_ctx, 2, stdout); /* Begin function. */ json_attr_object_begin (&json_ctx, FUNCNAME); for (int v = 0; v < NUM_VARIANTS; v++) { /* Run for approximately DURATION seconds. */ clock_gettime (CLOCK_MONOTONIC_RAW, &runtime); runtime.tv_sec += DURATION; double d_total_i = 0; timing_t total = 0, max = 0, min = 0x7fffffffffffffff; int64_t c = 0; while (1) { for (i = 0; i < NUM_SAMPLES (v); i++) { uint64_t cur; TIMING_NOW (start); for (k = 0; k < iters; k++) BENCH_FUNC (v, i); TIMING_NOW (end); TIMING_DIFF (cur, start, end); if (cur > max) max = cur; if (cur < min) min = cur; TIMING_ACCUM (total, cur); /* Accumulate timings for the value. In the end we will divide by the total iterations. */ RESULT_ACCUM (cur, v, i, c * iters, (c + 1) * iters); d_total_i += iters; } c++; struct timespec curtime; memset (&curtime, 0, sizeof (curtime)); clock_gettime (CLOCK_MONOTONIC_RAW, &curtime); if (TIMESPEC_AFTER (curtime, runtime)) goto done; } double d_total_s; double d_iters; done: d_total_s = total; d_iters = iters; /* Begin variant. */ json_attr_object_begin (&json_ctx, VARIANT (v)); json_attr_double (&json_ctx, "duration", d_total_s); json_attr_double (&json_ctx, "iterations", d_total_i); json_attr_double (&json_ctx, "max", max / d_iters); json_attr_double (&json_ctx, "min", min / d_iters); json_attr_double (&json_ctx, "mean", d_total_s / d_total_i); if (detailed) { json_array_begin (&json_ctx, "timings"); for (int i = 0; i < NUM_SAMPLES (v); i++) json_element_double (&json_ctx, RESULT (v, i)); json_array_end (&json_ctx); } /* End variant. */ json_attr_object_end (&json_ctx); } /* End function. */ json_attr_object_end (&json_ctx); return 0; }