phoboslab / qoi

The “Quite OK Image Format” for fast, lossless image compression
MIT License
6.94k stars 330 forks source link

Less hardcoded benchmarking code #269

Closed N-R-K closed 1 year ago

N-R-K commented 1 year ago

this allows people to more easily plugin their implementation for benchmarking purposes.

N-R-K commented 1 year ago

For some context: I wanted to plugin my own decoder to see how it performed. But I soon saw that there was no easy way for me to do that without duplicating/copy-pasting a bunch of code.

So the easiest way for me to plugin my own decoder was to steal the "stbi" slot:

@@ -445,14 +446,16 @@ benchmark_result_t benchmark_image(const char *path) {
                void *dec_p = libpng_decode(encoded_png, encoded_png_size, &dec_w, &dec_h);
                free(dec_p);
            });
-
-           BENCHMARK_FN(opt_nowarmup, opt_runs, res.stbi.decode_time, {
-               int dec_w, dec_h, dec_channels;
-               void *dec_p = stbi_load_from_memory(encoded_png, encoded_png_size, &dec_w, &dec_h, &dec_channels, 4);
-               free(dec_p);
-           });
        }

+       BENCHMARK_FN(opt_nowarmup, opt_runs, res.stbi.decode_time, {
+           QoiDecCtx qoi;
+           qoi_dec_init(&qoi, encoded_qoi, encoded_qoi_size);
+           qoi.data = malloc(qoi.data_size);
+           qoi_dec(&qoi);
+           free(qoi.data);
+       });
+

This "works" but obviously, the results will show my decoder as "stbi" - which isn't great ^^'

With the new changes, I can plug in my own decoder much more easily:

--- a/qoibench.c
+++ b/qoibench.c
@@ -28,6 +28,8 @@ Compile with:
 #include "qoi.h"

+#define QOIDEC_API static
+#include "qoi-dec.c"

 // -----------------------------------------------------------------------------
@@ -307,6 +309,7 @@ enum {
    LIBPNG,
    STBI,
    QOI,
+   QOIDEC,
    BENCH_COUNT /* must be the last element */
 };
 static const char *const lib_names[BENCH_COUNT] = {
@@ -314,6 +317,7 @@ static const char *const lib_names[BENCH_COUNT] = {
    [LIBPNG] =  "libpng: ",
    [STBI]   =  "stbi:   ",
    [QOI]    =  "qoi:    ",
+   [QOIDEC] =  "qoi-dec:",
 };

 typedef struct {
@@ -448,6 +452,14 @@ benchmark_result_t benchmark_image(const char *path) {
            void *dec_p = qoi_decode(encoded_qoi, encoded_qoi_size, &desc, 4);
            free(dec_p);
        });
+
+       BENCHMARK_FN(opt_nowarmup, opt_runs, res.libs[QOIDEC].decode_time, {
+           QoiDecCtx qoi;
+           qoi_dec_init(&qoi, encoded_qoi, encoded_qoi_size);
+           qoi.data = malloc(qoi.data_size);
+           qoi_dec(&qoi);
+           free(qoi.data);
+       });
    }
phoboslab commented 1 year ago

Neat!