Guest User

Untitled

a guest
Jan 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package sortingexample
  2.  
  3. import (
  4. "sort"
  5. "testing"
  6. )
  7.  
  8. // Example of struct we going to sort.
  9.  
  10. type Point struct {
  11. X, Y int
  12. }
  13.  
  14. // --- Struct / Raw Data
  15. var TestCases = []Point{
  16. {10, 3},
  17. {10, 4},
  18. {10, 35},
  19. {10, 5},
  20. {10, 51},
  21. {10, 25},
  22. {10, 59},
  23. {10, 15},
  24. {10, 22},
  25. {10, 91},
  26. }
  27.  
  28. // Example One - Sorting Slice Directly
  29. // somehow - slowest way to sort it.
  30. func SortSlice(points []Point) {
  31.  
  32. sorter := func(i, j int) bool {
  33. return points[i].Y <= points[j].Y
  34. }
  35.  
  36. sort.Slice(points, sorter)
  37. }
  38.  
  39. func BenchmarkSlice(b *testing.B) {
  40. tmp := make([]Point, len(TestCases))
  41. for i := 0; i < b.N; i++ {
  42. copy(tmp, TestCases)
  43. SortSlice(tmp)
  44. }
  45. }
  46.  
  47. // Example Two - Sorting Slice Directly
  48. // much faster performance
  49. type Points []Point
  50.  
  51. // Sort interface implementation
  52. func (p Points) Less(i, j int) bool { return p[i].Y <= p[j].Y }
  53. func (p Points) Len() int { return len(p) }
  54. func (p Points) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  55.  
  56. func SortStruct(points []Point) {
  57. sort.Sort(Points(points))
  58. }
  59.  
  60. func BenchmarkStruct(b *testing.B) {
  61. tmp := make([]Point, len(TestCases))
  62. for i := 0; i < b.N; i++ {
  63. copy(tmp, TestCases)
  64. SortStruct(tmp)
  65. }
  66. }
  67.  
  68. // --- Pointers
  69. var TestCasesPoints = []*Point{
  70. &Point{10, 3},
  71. &Point{10, 4},
  72. &Point{10, 35},
  73. &Point{10, 5},
  74. &Point{10, 51},
  75. &Point{10, 25},
  76. &Point{10, 59},
  77. &Point{10, 15},
  78. &Point{10, 22},
  79. &Point{10, 91},
  80. }
  81.  
  82. // Example Three - Sorting Slice of Pointers
  83.  
  84. func SortSlicePointers(points []*Point) {
  85. sorter := func(i, j int) bool {
  86. return points[i].Y <= points[j].Y
  87. }
  88.  
  89. sort.Slice(points, sorter)
  90. }
  91.  
  92. func BenchmarkSlicePointers(b *testing.B) {
  93. tmp := make([]*Point, len(TestCasesPoints))
  94. for i := 0; i < b.N; i++ {
  95. copy(tmp, TestCasesPoints)
  96. SortSlicePointers(tmp)
  97. }
  98. }
  99.  
  100. // Example Four - Sorting Struct (with Slice of pointers beneath it)
  101. type PointsPointer []*Point
  102.  
  103. func (pp PointsPointer) Less(i, j int) bool { return pp[i].Y <= pp[j].Y }
  104. func (pp PointsPointer) Len() int { return len(pp) }
  105. func (pp PointsPointer) Swap(i, j int) { pp[i], pp[j] = pp[j], pp[i] }
  106.  
  107. func SortStructOfSlicePointers(points []*Point) {
  108. sort.Sort(PointsPointer(points))
  109. }
  110.  
  111. func BenchmarkStructOfSlicePointers(b *testing.B) {
  112. tmp := make([]*Point, len(TestCasesPoints))
  113.  
  114. for i := 0; i < b.N; i++ {
  115. copy(tmp, TestCasesPoints)
  116. SortStructOfSlicePointers(tmp)
  117. }
  118. }
  119.  
  120. > go test -bench=.
  121. goos: darwin
  122. goarch: amd64
  123. BenchmarkSlice-4 3000000 542 ns/op
  124. BenchmarkStruct-4 5000000 318 ns/op
  125. BenchmarkSlicePointers-4 5000000 280 ns/op
  126. BenchmarkStructOfSlicePointers-4 5000000 321 ns/op
Add Comment
Please, Sign In to add comment