1 /* See LICENSE file for copyright and license details. */
9 #include "../grapheme.h"
10 #include "../gen/character-test.h"
15 #define NUM_ITERATIONS 100000
17 struct utf8_benchmark_payload {
19 utf8proc_uint8_t *buf_utf8proc;
24 libgrapheme(const void *payload)
26 const struct utf8_benchmark_payload *p = payload;
30 for (off = 0; off < p->buflen; off += ret) {
31 if ((ret = grapheme_decode_utf8(p->buf + off,
32 p->buflen - off, &cp)) >
41 libutf8proc(const void *payload)
43 const struct utf8_benchmark_payload *p = payload;
48 for (off = 0; off < p->buflen; off += (size_t)ret) {
49 if ((ret = utf8proc_iterate(p->buf_utf8proc + off,
50 (utf8proc_ssize_t)(p->buflen - off),
59 main(int argc, char *argv[])
61 struct utf8_benchmark_payload p;
63 double baseline = (double)NAN;
67 p.buf = generate_utf8_test_buffer(character_break_test,
68 LEN(character_break_test),
71 /* convert cp-buffer to stupid custom libutf8proc-uint8-type */
72 if ((p.buf_utf8proc = malloc(p.buflen)) == NULL) {
73 fprintf(stderr, "malloc: %s\n", strerror(errno));
76 for (i = 0; i < p.buflen; i++) {
78 * even if char is larger than 8 bit, it will only have
79 * any of the first 8 bits set (by construction).
81 p.buf_utf8proc[i] = (utf8proc_uint8_t)p.buf[i];
84 printf("%s\n", argv[0]);
85 run_benchmark(libgrapheme, &p, "libgrapheme ", NULL,
86 "byte", &baseline, NUM_ITERATIONS, p.buflen);
87 run_benchmark(libutf8proc, &p, "libutf8proc ",
88 "but unsafe (does not detect overlong encodings)",
89 "byte", &baseline, NUM_ITERATIONS, p.buflen);