benchmark_strings.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package benchmark_strings
  2. import "base:intrinsics"
  3. import "core:fmt"
  4. import "core:log"
  5. import "core:testing"
  6. import "core:strings"
  7. import "core:text/table"
  8. import "core:time"
  9. import "core:unicode/utf8"
  10. RUNS_PER_SIZE :: 2500
  11. sizes := [?]int {
  12. 7, 8, 9,
  13. 15, 16, 17,
  14. 31, 32, 33,
  15. 63, 64, 65,
  16. 95, 96, 97,
  17. 128,
  18. 256,
  19. 512,
  20. 1024,
  21. 4096,
  22. }
  23. // These are the normal, unoptimized algorithms.
  24. plain_prefix_length :: proc "contextless" (a, b: string) -> (n: int) {
  25. _len := min(len(a), len(b))
  26. // Scan for matches including partial codepoints.
  27. #no_bounds_check for n < _len && a[n] == b[n] {
  28. n += 1
  29. }
  30. // Now scan to ignore partial codepoints.
  31. if n > 0 {
  32. s := a[:n]
  33. n = 0
  34. for {
  35. r0, w := utf8.decode_rune(s[n:])
  36. if r0 != utf8.RUNE_ERROR {
  37. n += w
  38. } else {
  39. break
  40. }
  41. }
  42. }
  43. return
  44. }
  45. run_trial_size_prefix :: proc(p: proc "contextless" (string, string) -> $R, suffix: string, size: int, idx: int, runs: int, loc := #caller_location) -> (timing: time.Duration) {
  46. left := make([]u8, size)
  47. right := make([]u8, size)
  48. defer {
  49. delete(left)
  50. delete(right)
  51. }
  52. if len(suffix) > 0 {
  53. copy(left [idx:], suffix)
  54. copy(right[idx:], suffix)
  55. } else {
  56. right[idx] = 'A'
  57. }
  58. accumulator: int
  59. watch: time.Stopwatch
  60. time.stopwatch_start(&watch)
  61. for _ in 0..<runs {
  62. result := p(string(left[:size]), string(right[:size]))
  63. accumulator += result
  64. }
  65. time.stopwatch_stop(&watch)
  66. timing = time.stopwatch_duration(watch)
  67. log.debug(accumulator)
  68. return
  69. }
  70. run_trial_size :: proc {
  71. run_trial_size_prefix,
  72. }
  73. bench_table_size :: proc(algo_name: string, plain, simd: $P, suffix := "") {
  74. string_buffer := strings.builder_make()
  75. defer strings.builder_destroy(&string_buffer)
  76. tbl: table.Table
  77. table.init(&tbl)
  78. defer table.destroy(&tbl)
  79. table.aligned_header_of_values(&tbl, .Right, "Algorithm", "Size", "Iterations", "Scalar", "SIMD", "SIMD Relative (%)", "SIMD Relative (x)")
  80. for size in sizes {
  81. // Place the non-zero byte somewhere in the middle.
  82. needle_index := size / 2
  83. plain_timing := run_trial_size(plain, suffix, size, needle_index, RUNS_PER_SIZE)
  84. simd_timing := run_trial_size(simd, suffix, size, needle_index, RUNS_PER_SIZE)
  85. _plain := fmt.tprintf("%8M", plain_timing)
  86. _simd := fmt.tprintf("%8M", simd_timing)
  87. _relp := fmt.tprintf("%.3f %%", f64(simd_timing) / f64(plain_timing) * 100.0)
  88. _relx := fmt.tprintf("%.3f x", 1 / (f64(simd_timing) / f64(plain_timing)))
  89. table.aligned_row_of_values(
  90. &tbl,
  91. .Right,
  92. algo_name,
  93. size, RUNS_PER_SIZE, _plain, _simd, _relp, _relx)
  94. }
  95. builder_writer := strings.to_writer(&string_buffer)
  96. fmt.sbprintln(&string_buffer)
  97. table.write_plain_table(builder_writer, &tbl)
  98. my_table_string := strings.to_string(string_buffer)
  99. log.info(my_table_string)
  100. }
  101. @test
  102. benchmark_memory_procs :: proc(t: ^testing.T) {
  103. bench_table_size("prefix_length ascii", plain_prefix_length, strings.prefix_length)
  104. bench_table_size("prefix_length unicode", plain_prefix_length, strings.prefix_length, "🦉")
  105. }