| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- /*
- * Copyright © 2021 Ran Benita <ran@unusedvar.com>
- * SPDX-License-Identifier: MIT
- */
- #include "config.h"
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "atom.h"
- #include "bench.h"
- #include "darray.h"
- #define BENCHMARK_ITERATIONS 100
- int
- main(void)
- {
- int ret = EXIT_SUCCESS;
- FILE *file;
- char wordbuf[1024];
- darray(char *) words;
- char **worditer;
- struct atom_table *table;
- xkb_atom_t atom;
- const char *text;
- struct bench bench;
- char *elapsed;
- darray_init(words);
- file = fopen("/usr/share/dict/words", "rb");
- if (file == NULL) {
- perror("/usr/share/dict/words");
- return -1;
- }
- while (fgets(wordbuf, sizeof(wordbuf), file)) {
- size_t len = strlen(wordbuf);
- if (len > 0 && wordbuf[len - 1] == '\n')
- wordbuf[len - 1] = '\0';
- char *word = strdup(wordbuf);
- if (!word) {
- fclose(file);
- ret = EXIT_FAILURE;
- goto out;
- }
- darray_append(words, word);
- }
- fclose(file);
- bench_start(&bench);
- for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
- table = atom_table_new();
- assert(table);
- darray_foreach(worditer, words) {
- atom = atom_intern(table, *worditer, strlen(*worditer) - 1, true);
- assert(atom != XKB_ATOM_NONE);
- text = atom_text(table, atom);
- assert(text != NULL);
- }
- atom_table_free(table);
- }
- bench_stop(&bench);
- elapsed = bench_elapsed_str(&bench);
- fprintf(stderr, "%d iterations in %ss\n",
- BENCHMARK_ITERATIONS, elapsed);
- free(elapsed);
- out:
- darray_foreach(worditer, words) {
- free(*worditer);
- }
- darray_free(words);
- return ret;
- }
|