benchmark_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package help
  2. import "testing"
  3. const (
  4. size = 1000000
  5. )
  6. func BenchmarkSelectWhereFirst(b *testing.B) {
  7. for n := 0; n < b.N; n++ {
  8. Range(1, size).Select(func(i interface{}) interface{} {
  9. return -i.(int)
  10. }).Where(func(i interface{}) bool {
  11. return i.(int) > -1000
  12. }).First()
  13. }
  14. }
  15. func BenchmarkSelectWhereFirst_generics(b *testing.B) {
  16. for n := 0; n < b.N; n++ {
  17. Range(1, size).SelectT(func(i int) int {
  18. return -i
  19. }).WhereT(func(i int) bool {
  20. return i > -1000
  21. }).First()
  22. }
  23. }
  24. func BenchmarkSum(b *testing.B) {
  25. for n := 0; n < b.N; n++ {
  26. Range(1, size).Where(func(i interface{}) bool {
  27. return i.(int)%2 == 0
  28. }).SumInts()
  29. }
  30. }
  31. func BenchmarkSum_generics(b *testing.B) {
  32. for n := 0; n < b.N; n++ {
  33. Range(1, size).WhereT(func(i int) bool {
  34. return i%2 == 0
  35. }).SumInts()
  36. }
  37. }
  38. func BenchmarkZipSkipTake(b *testing.B) {
  39. for n := 0; n < b.N; n++ {
  40. Range(1, size).Zip(Range(1, size).Select(func(i interface{}) interface{} {
  41. return i.(int) * 2
  42. }), func(i, j interface{}) interface{} {
  43. return i.(int) + j.(int)
  44. }).Skip(2).Take(5)
  45. }
  46. }
  47. func BenchmarkZipSkipTake_generics(b *testing.B) {
  48. for n := 0; n < b.N; n++ {
  49. Range(1, size).ZipT(Range(1, size).SelectT(func(i int) int {
  50. return i * 2
  51. }), func(i, j int) int {
  52. return i + j
  53. }).Skip(2).Take(5)
  54. }
  55. }
  56. func BenchmarkFromChannel(b *testing.B) {
  57. for n := 0; n < b.N; n++ {
  58. ch := make(chan interface{})
  59. go func() {
  60. for i := 0; i < size; i++ {
  61. ch <- i
  62. }
  63. close(ch)
  64. }()
  65. FromChannel(ch).All(func(i interface{}) bool { return true })
  66. }
  67. }
  68. func BenchmarkFromChannelT(b *testing.B) {
  69. for n := 0; n < b.N; n++ {
  70. ch := make(chan interface{})
  71. go func() {
  72. for i := 0; i < size; i++ {
  73. ch <- i
  74. }
  75. close(ch)
  76. }()
  77. FromChannelT(ch).All(func(i interface{}) bool { return true })
  78. }
  79. }