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 break_benchmark_payload {
18 3448adb0 2022-11-02 op uint_least32_t *buf;
19 3448adb0 2022-11-02 op utf8proc_int32_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 uint_least16_t state = 0;
27 3448adb0 2022-11-02 op const struct break_benchmark_payload *p = payload;
30 3448adb0 2022-11-02 op for (i = 0; i + 1 < p->buflen; i++) {
31 3448adb0 2022-11-02 op (void)grapheme_is_character_break(p->buf[i], p->buf[i+1],
37 3448adb0 2022-11-02 op libutf8proc(const void *payload)
39 3448adb0 2022-11-02 op utf8proc_int32_t state = 0;
40 3448adb0 2022-11-02 op const struct break_benchmark_payload *p = payload;
43 3448adb0 2022-11-02 op for (i = 0; i + 1 < p->buflen; i++) {
44 3448adb0 2022-11-02 op (void)utf8proc_grapheme_break_stateful(p->buf_utf8proc[i],
45 3448adb0 2022-11-02 op p->buf_utf8proc[i+1],
51 3448adb0 2022-11-02 op main(int argc, char *argv[])
53 3448adb0 2022-11-02 op struct break_benchmark_payload p;
54 3448adb0 2022-11-02 op double baseline = (double)NAN;
59 3448adb0 2022-11-02 op if ((p.buf = generate_cp_test_buffer(character_break_test,
60 3448adb0 2022-11-02 op LEN(character_break_test),
61 3448adb0 2022-11-02 op &(p.buflen))) == NULL) {
64 3448adb0 2022-11-02 op if ((p.buf_utf8proc = malloc(p.buflen * sizeof(*(p.buf_utf8proc)))) == NULL) {
65 3448adb0 2022-11-02 op fprintf(stderr, "malloc: %s\n", strerror(errno));
68 3448adb0 2022-11-02 op for (i = 0; i < p.buflen; i++) {
70 3448adb0 2022-11-02 op * there is no overflow, as we know that the maximum
71 3448adb0 2022-11-02 op * codepoint is 0x10FFFF, which is way below 2^31
73 3448adb0 2022-11-02 op p.buf_utf8proc[i] = (utf8proc_int32_t)p.buf[i];
76 3448adb0 2022-11-02 op printf("%s\n", argv[0]);
77 3448adb0 2022-11-02 op run_benchmark(libgrapheme, &p, "libgrapheme ", NULL, "comparison",
78 3448adb0 2022-11-02 op &baseline, NUM_ITERATIONS, p.buflen - 1);
79 3448adb0 2022-11-02 op run_benchmark(libutf8proc, &p, "libutf8proc ", NULL, "comparison",
80 3448adb0 2022-11-02 op &baseline, NUM_ITERATIONS, p.buflen - 1);
83 3448adb0 2022-11-02 op free(p.buf_utf8proc);