1
0

keysym-case-mappings.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright © 2023 Pierre Le Marre
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include <time.h>
  7. #include <stdbool.h>
  8. #include "xkbcommon/xkbcommon.h"
  9. #include "src/keysym.h"
  10. #include "../test/test.h"
  11. #include "bench.h"
  12. #define BENCHMARK_ITERATIONS 300
  13. typedef uint32_t (*CaseMappingFunc)(xkb_keysym_t ks);
  14. typedef bool (*CaseTestFunc)(xkb_keysym_t ks);
  15. struct TestedFunction {
  16. union {
  17. struct {
  18. CaseMappingFunc toLower;
  19. CaseMappingFunc toUpper;
  20. };
  21. struct {
  22. CaseTestFunc isLower;
  23. CaseTestFunc isUpper;
  24. };
  25. };
  26. const char *name;
  27. };
  28. static const struct TestedFunction functions[] = {
  29. { {.toLower = xkb_keysym_to_lower, .toUpper = xkb_keysym_to_upper},
  30. "to_lower & to_upper" },
  31. { {.isLower = xkb_keysym_is_lower, .isUpper = xkb_keysym_is_upper_or_title},
  32. "is_lower & is_upper" },
  33. };
  34. int
  35. main(void)
  36. {
  37. struct bench bench;
  38. for (size_t f = 0; f < ARRAY_SIZE(functions); f++) {
  39. for (int explicit = 1; explicit >= 0; explicit--) {
  40. fprintf(stderr, "Benchmarking %s...\n", functions[f].name);
  41. bench_start(&bench);
  42. for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
  43. struct xkb_keysym_iterator *iter = xkb_keysym_iterator_new(explicit);
  44. while (xkb_keysym_iterator_next(iter)) {
  45. xkb_keysym_t ks = xkb_keysym_iterator_get_keysym(iter);
  46. functions[f].toLower(ks);
  47. functions[f].toUpper(ks);
  48. }
  49. /* Avoid dangling pointers
  50. * NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) */
  51. iter = xkb_keysym_iterator_unref(iter);
  52. }
  53. bench_stop(&bench);
  54. char *elapsed = bench_elapsed_str(&bench);
  55. fprintf(stderr,
  56. "Applied %d times \"%s\" to %s assigned keysyms in %ss\n",
  57. BENCHMARK_ITERATIONS, functions[f].name, explicit ? "explicitly" : "all", elapsed);
  58. free(elapsed);
  59. }
  60. }
  61. return 0;
  62. }