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