atom.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright © 2021 Ran Benita <ran@unusedvar.com>
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include "atom.h"
  13. #include "bench.h"
  14. #include "darray.h"
  15. #define BENCHMARK_ITERATIONS 100
  16. int
  17. main(void)
  18. {
  19. int ret = EXIT_SUCCESS;
  20. FILE *file;
  21. char wordbuf[1024];
  22. darray(char *) words;
  23. char **worditer;
  24. struct atom_table *table;
  25. xkb_atom_t atom;
  26. const char *text;
  27. struct bench bench;
  28. char *elapsed;
  29. darray_init(words);
  30. file = fopen("/usr/share/dict/words", "rb");
  31. if (file == NULL) {
  32. perror("/usr/share/dict/words");
  33. return -1;
  34. }
  35. while (fgets(wordbuf, sizeof(wordbuf), file)) {
  36. size_t len = strlen(wordbuf);
  37. if (len > 0 && wordbuf[len - 1] == '\n')
  38. wordbuf[len - 1] = '\0';
  39. char *word = strdup(wordbuf);
  40. if (!word) {
  41. fclose(file);
  42. ret = EXIT_FAILURE;
  43. goto out;
  44. }
  45. darray_append(words, word);
  46. }
  47. fclose(file);
  48. bench_start(&bench);
  49. for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
  50. table = atom_table_new();
  51. assert(table);
  52. darray_foreach(worditer, words) {
  53. atom = atom_intern(table, *worditer, strlen(*worditer) - 1, true);
  54. assert(atom != XKB_ATOM_NONE);
  55. text = atom_text(table, atom);
  56. assert(text != NULL);
  57. }
  58. atom_table_free(table);
  59. }
  60. bench_stop(&bench);
  61. elapsed = bench_elapsed_str(&bench);
  62. fprintf(stderr, "%d iterations in %ss\n",
  63. BENCHMARK_ITERATIONS, elapsed);
  64. free(elapsed);
  65. out:
  66. darray_foreach(worditer, words) {
  67. free(*worditer);
  68. }
  69. darray_free(words);
  70. return ret;
  71. }