[hackers] [libgrapheme] Generalize benchmark-function with payload-struct || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Tue, 4 Jan 2022 18:10:41 +0100 (CET)

commit e7b4a99ac11124212811a345563e65d199b1fb79
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Tue Jan 4 18:10:09 2022 +0100
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Tue Jan 4 18:10:09 2022 +0100

    Generalize benchmark-function with payload-struct
    
    Signed-off-by: Laslo Hunhold <dev_AT_frign.de>

diff --git a/benchmark/character.c b/benchmark/character.c
index 84ea33f..57d7990 100644
--- a/benchmark/character.c
+++ b/benchmark/character.c
_AT_@ -8,61 +8,68 @@
 #include "../gen/character-test.h"
 #include "util.h"
 
-#include <unigbrk.h>
 #include <utf8proc.h>
 
-#define NUM_ITERATIONS 10000
+#define NUM_ITERATIONS 100000
 
 #if defined __has_attribute
         #if __has_attribute(optnone)
- void libgrapheme(const uint32_t *, size_t) __attribute__((optnone));
- void libutf8proc(const uint32_t *, size_t) __attribute__((optnone));
+ void libgrapheme(const void *) __attribute__((optnone));
+ void libutf8proc(const void *) __attribute__((optnone));
         #endif
 #endif
 
+struct payload {
+ uint_least32_t *buf;
+ size_t bufsiz;
+};
+
 void
-libgrapheme(const uint32_t *buf, size_t bufsiz)
+libgrapheme(const void *payload)
 {
         GRAPHEME_STATE state = { 0 };
+ const struct payload *p = payload;
         size_t i;
 
- for (i = 0; i + 1 < bufsiz; i++) {
- (void)grapheme_is_character_break(buf[i], buf[i+1], &state);
+ for (i = 0; i + 1 < p->bufsiz; i++) {
+ (void)grapheme_is_character_break(p->buf[i], p->buf[i+1],
+ &state);
         }
 }
 
 void
-libutf8proc(const uint32_t *buf, size_t bufsiz)
+libutf8proc(const void *payload)
 {
         utf8proc_int32_t state = 0;
+ const struct payload *p = payload;
         size_t i;
 
- for (i = 0; i + 1 < bufsiz; i++) {
- (void)utf8proc_grapheme_break_stateful(buf[i], buf[i+1], &state);
+ for (i = 0; i + 1 < p->bufsiz; i++) {
+ (void)utf8proc_grapheme_break_stateful(p->buf[i], p->buf[i+1],
+ &state);
         }
 }
 
 int
 main(int argc, char *argv[])
 {
- size_t bufsiz;
- uint32_t *buf;
+ struct payload p;
         double baseline = NAN;
 
         (void)argc;
 
- if ((buf = generate_test_buffer(character_test, LEN(character_test),
- &bufsiz)) == NULL) {
+ if ((p.buf = generate_test_buffer(character_test, LEN(character_test),
+ &(p.bufsiz))) == NULL) {
                 return 1;
         }
 
         printf("%s\n", argv[0]);
- run_benchmark(libgrapheme, "libgrapheme ", &baseline, buf, bufsiz,
+ run_benchmark(libgrapheme, &p, "libgrapheme ", &baseline,
                       NUM_ITERATIONS);
- run_benchmark(libutf8proc, "libutf8proc ", &baseline, buf, bufsiz,
+ run_benchmark(libutf8proc, &p, "libutf8proc ", &baseline,
                       NUM_ITERATIONS);
 
- free(buf);
+ free(p.buf);
 
         return 0;
 }
diff --git a/benchmark/util.c b/benchmark/util.c
index a30196d..291eadd 100644
--- a/benchmark/util.c
+++ b/benchmark/util.c
_AT_@ -38,9 +38,8 @@ time_diff(struct timespec *a, struct timespec *b)
 }
 
 void
-run_benchmark(void (*func)(const uint32_t *, size_t), const char *name,
- double *baseline, const uint32_t *buf, size_t bufsiz,
- uint32_t num_iterations)
+run_benchmark(void (*func)(const void *), const void *payload,
+ const char *name, double *baseline, uint32_t num_iterations)
 {
         struct timespec start, end;
         size_t i;
_AT_@ -51,7 +50,7 @@ run_benchmark(void (*func)(const uint32_t *, size_t), const char *name,
 
         clock_gettime(CLOCK_MONOTONIC, &start);
         for (i = 0; i < num_iterations; i++) {
- func(buf, bufsiz);
+ func(payload);
 
                 if (i % (num_iterations / 10) == 0) {
                         printf(".");
_AT_@ -59,13 +58,13 @@ run_benchmark(void (*func)(const uint32_t *, size_t), const char *name,
                 }
         }
         clock_gettime(CLOCK_MONOTONIC, &end);
- diff = time_diff(&start, &end);
+ diff = time_diff(&start, &end) / num_iterations;
 
         if (isnan(*baseline)) {
                 *baseline = diff;
- printf(" %.3fs (baseline)\n", diff);
+ printf(" avg. %.3es (baseline)\n", diff);
         } else {
- printf(" %.3fs (%.2f%% %s)\n", diff,
+ printf(" avg. %.3es (%.2f%% %s)\n", diff,
                        fabs(1.0 - diff / *baseline) * 100,
                        (diff < *baseline) ? "faster" : "slower");
         }
diff --git a/benchmark/util.h b/benchmark/util.h
index 13320cc..380d48c 100644
--- a/benchmark/util.h
+++ b/benchmark/util.h
_AT_@ -7,7 +7,7 @@
 #define LEN(x) (sizeof(x) / sizeof(*(x)))
 
 uint32_t *generate_test_buffer(const struct test *, size_t, size_t *);
-void run_benchmark(void (*func)(const uint32_t *, size_t), const char *,
- double *, const uint32_t *, size_t, uint32_t);
+void run_benchmark(void (*func)(const void *), const void *, const char *,
+ double *, uint32_t);
 
 #endif /* UTIL_H */
Received on Tue Jan 04 2022 - 18:10:41 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 04 2022 - 18:12:32 CET