benchmark_bytes.odin 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package benchmark_bytes
  2. import "core:bytes"
  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. RUNS_PER_SIZE :: 2500
  10. sizes := [?]int {
  11. 15, 16, 17,
  12. 31, 32, 33,
  13. 63, 64, 65,
  14. 128,
  15. 256,
  16. 512,
  17. 1024,
  18. 4096,
  19. 1024 * 1024,
  20. // 1024 * 1024 * 1024,
  21. }
  22. // These are the normal, unoptimized algorithms.
  23. plain_index_byte :: proc "contextless" (s: []u8, c: byte) -> (res: int) #no_bounds_check {
  24. for i := 0; i < len(s); i += 1 {
  25. if s[i] == c {
  26. return i
  27. }
  28. }
  29. return -1
  30. }
  31. plain_last_index_byte :: proc "contextless" (s: []u8, c: byte) -> (res: int) #no_bounds_check {
  32. for i := len(s)-1; i >= 0; i -= 1 {
  33. if s[i] == c {
  34. return i
  35. }
  36. }
  37. return -1
  38. }
  39. run_trial_size :: proc(p: proc "contextless" ([]u8, byte) -> int, size: int, idx: int, runs: int) -> (timing: time.Duration) {
  40. data := make([]u8, size)
  41. defer delete(data)
  42. for i in 0..<size {
  43. data[i] = u8('0' + i % 10)
  44. }
  45. data[idx] = 'z'
  46. accumulator: int
  47. watch: time.Stopwatch
  48. time.stopwatch_start(&watch)
  49. for _ in 0..<runs {
  50. accumulator += p(data, 'z')
  51. }
  52. time.stopwatch_stop(&watch)
  53. timing = time.stopwatch_duration(watch)
  54. log.debug(accumulator)
  55. return
  56. }
  57. bench_table :: proc(algo_name: string, forward: bool, plain: proc "contextless" ([]u8, byte) -> int, simd: proc "contextless" ([]u8, byte) -> int) {
  58. string_buffer := strings.builder_make()
  59. defer strings.builder_destroy(&string_buffer)
  60. tbl: table.Table
  61. table.init(&tbl)
  62. defer table.destroy(&tbl)
  63. // table.caption(&tbl, "index_byte benchmark")
  64. table.aligned_header_of_values(&tbl, .Right, "Algorithm", "Size", "Iterations", "Scalar", "SIMD", "SIMD Relative (%)", "SIMD Relative (x)")
  65. for size in sizes {
  66. needle_index := size - 1 if forward else 0
  67. plain_timing := run_trial_size(plain, size, needle_index, RUNS_PER_SIZE)
  68. simd_timing := run_trial_size(simd, size, needle_index, RUNS_PER_SIZE)
  69. _plain := fmt.tprintf("%8M", plain_timing)
  70. _simd := fmt.tprintf("%8M", simd_timing)
  71. _relp := fmt.tprintf("%.3f %%", f64(simd_timing) / f64(plain_timing) * 100.0)
  72. _relx := fmt.tprintf("%.3f x", 1 / (f64(simd_timing) / f64(plain_timing)))
  73. table.aligned_row_of_values(
  74. &tbl,
  75. .Right,
  76. algo_name,
  77. size, RUNS_PER_SIZE, _plain, _simd, _relp, _relx)
  78. }
  79. builder_writer := strings.to_writer(&string_buffer)
  80. fmt.sbprintln(&string_buffer)
  81. table.write_plain_table(builder_writer, &tbl)
  82. my_table_string := strings.to_string(string_buffer)
  83. log.info(my_table_string)
  84. }
  85. @test
  86. benchmark_index_byte :: proc(t: ^testing.T) {
  87. bench_table("index_byte", true, plain_index_byte, bytes.index_byte)
  88. // bench_table("last_index_byte", false, plain_last_index_byte, bytes.last_index_byte)
  89. }
  90. /*
  91. @test
  92. benchmark_simd_index_hot :: proc(t: ^testing.T) {
  93. report: string
  94. for size in sizes {
  95. timing := run_trial_size(bytes.index_byte, size, size - 1, HOT, HOT)
  96. report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
  97. timing = run_trial_size(bytes.last_index_byte, size, 0, HOT, HOT)
  98. report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
  99. }
  100. log.info(report)
  101. }
  102. */