character-performance.c (1472B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdint.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <time.h> 7 8 #include "../grapheme.h" 9 #include "../gen/character-test.h" 10 #include "util.h" 11 12 #define NUM_ITERATIONS 1000 13 14 int 15 main(int argc, char *argv[]) 16 { 17 struct timespec start, end; 18 size_t i, j, bufsiz, off; 19 uint32_t *buf; 20 GRAPHEME_STATE state; 21 double cp_per_sec; 22 23 (void)argc; 24 25 /* allocate and generate buffer */ 26 for (i = 0, bufsiz = 0; i < LEN(character_test); i++) { 27 bufsiz += character_test[i].cplen; 28 } 29 if (!(buf = calloc(bufsiz, sizeof(*buf)))) { 30 fprintf(stderr, "%s: calloc: Out of memory.\n", argv[0]); 31 return 1; 32 } 33 for (i = 0, off = 0; i < LEN(character_test); i++) { 34 for (j = 0; j < character_test[i].cplen; j++) { 35 buf[off + j] = character_test[i].cp[j]; 36 } 37 off += character_test[i].cplen; 38 } 39 40 /* run test */ 41 printf("%s: Running benchmark ", argv[0]); 42 fflush(stdout); 43 44 clock_gettime(CLOCK_MONOTONIC, &start); 45 for (i = 0; i < NUM_ITERATIONS; i++) { 46 memset(&state, 0, sizeof(state)); 47 for (j = 0; j < bufsiz - 1; j++) { 48 (void)grapheme_is_character_break(buf[j], buf[j+1], 49 &state); 50 } 51 if (i % (NUM_ITERATIONS / 10) == 0) { 52 printf("."); 53 fflush(stdout); 54 } 55 } 56 clock_gettime(CLOCK_MONOTONIC, &end); 57 58 cp_per_sec = (double)(NUM_ITERATIONS * bufsiz) / 59 time_diff(&start, &end); 60 61 printf(" %.2e CP/s\n", cp_per_sec); 62 63 return 0; 64 }