benchmarks_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // License: GPLv3 Copyright: 2024, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package simdstring
  3. import (
  4. "bytes"
  5. "fmt"
  6. "testing"
  7. )
  8. var _ = fmt.Print
  9. func haystack(filler, needle byte, pos int) []byte {
  10. var data []byte
  11. if pos > 0 {
  12. data = append(bytes.Repeat([]byte{filler}, pos-1), needle)
  13. } else {
  14. data = []byte{needle}
  15. }
  16. return data
  17. }
  18. var sizes = []int{6, 327, 9875, 1198673}
  19. func BenchmarkIndexByte(b *testing.B) {
  20. t := func(pos int, which string) {
  21. data := haystack('a', 'q', pos)
  22. f := IndexByte
  23. switch which {
  24. case "scalar":
  25. f = index_byte_scalar
  26. case "stdlib":
  27. f = bytes.IndexByte
  28. }
  29. b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
  30. for i := 0; i < b.N; i++ {
  31. f(data, 'q')
  32. }
  33. })
  34. }
  35. for _, pos := range sizes {
  36. t(pos, "simdstring")
  37. t(pos, "scalar")
  38. t(pos, "stdlib")
  39. }
  40. }
  41. func BenchmarkIndexByte2(b *testing.B) {
  42. t := func(pos int, which string) {
  43. data := haystack('a', 'q', pos)
  44. f := IndexByte2
  45. switch which {
  46. case "scalar":
  47. f = index_byte2_scalar
  48. }
  49. b.Run(fmt.Sprintf("%s_sz=%d", which, pos), func(b *testing.B) {
  50. for i := 0; i < b.N; i++ {
  51. f(data, 'q', 'x')
  52. }
  53. })
  54. }
  55. for _, pos := range sizes {
  56. t(pos, "simdstring")
  57. t(pos, "scalar")
  58. }
  59. }