Advertisement
Guest User

Untitled

a guest
May 28th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. package bench
  2.  
  3. import (
  4. "fmt"
  5. "strings"
  6. "testing"
  7. )
  8.  
  9. func GenMap(count int) map[string]string {
  10. xrange := make([]struct{}, count)
  11. res := make(map[string]string, count)
  12. for i := range xrange {
  13. key := fmt.Sprintf("key:%d", i)
  14. value := fmt.Sprintf("value:%d", i)
  15. res[key] = value
  16. }
  17.  
  18. return res
  19. }
  20.  
  21. func SlowLabels(m map[string]string) string {
  22. strs := []string{}
  23. for k, v := range m {
  24. strs = append(strs, k+"="+v)
  25. }
  26.  
  27. return strings.Join(strs, ", ")
  28. }
  29.  
  30. func FastLabels(m map[string]string) string {
  31. strs := make([]string, 0, len(m))
  32. for k, v := range m {
  33. strs = append(strs, k+"="+v)
  34. }
  35.  
  36. return strings.Join(strs, ", ")
  37. }
  38.  
  39. func Benchmark100S(b *testing.B) {
  40. input := GenMap(100)
  41. for n := 0; n < b.N; n++ {
  42. SlowLabels(input)
  43. }
  44. }
  45.  
  46. func Benchmark100F(b *testing.B) {
  47. input := GenMap(100)
  48. for n := 0; n < b.N; n++ {
  49. FastLabels(input)
  50. }
  51. }
  52.  
  53. func Benchmark10S(b *testing.B) {
  54. input := GenMap(10)
  55. for n := 0; n < b.N; n++ {
  56. SlowLabels(input)
  57. }
  58. }
  59.  
  60. func Benchmark10F(b *testing.B) {
  61. input := GenMap(10)
  62. for n := 0; n < b.N; n++ {
  63. FastLabels(input)
  64. }
  65. }
  66.  
  67. func Benchmark5S(b *testing.B) {
  68. input := GenMap(5)
  69. for n := 0; n < b.N; n++ {
  70. SlowLabels(input)
  71. }
  72. }
  73.  
  74. func Benchmark5F(b *testing.B) {
  75. input := GenMap(5)
  76. for n := 0; n < b.N; n++ {
  77. FastLabels(input)
  78. }
  79. }
  80.  
  81. func Benchmark3S(b *testing.B) {
  82. input := GenMap(3)
  83. for n := 0; n < b.N; n++ {
  84. SlowLabels(input)
  85. }
  86. }
  87.  
  88. func Benchmark3F(b *testing.B) {
  89. input := GenMap(3)
  90. for n := 0; n < b.N; n++ {
  91. FastLabels(input)
  92. }
  93. }
  94.  
  95. func Benchmark1S(b *testing.B) {
  96. input := GenMap(1)
  97. for n := 0; n < b.N; n++ {
  98. SlowLabels(input)
  99. }
  100. }
  101.  
  102. func Benchmark1F(b *testing.B) {
  103. input := GenMap(1)
  104. for n := 0; n < b.N; n++ {
  105. FastLabels(input)
  106. }
  107. }
  108.  
  109. /*
  110. Benchmark100S 50000 30674 ns/op
  111. Benchmark100F 50000 26230 ns/op
  112. Benchmark10S 300000 4226 ns/op
  113. Benchmark10F 500000 2911 ns/op
  114. Benchmark5S 1000000 2123 ns/op
  115. Benchmark5F 1000000 1554 ns/op
  116. Benchmark3S 1000000 1423 ns/op
  117. Benchmark3F 1000000 1048 ns/op
  118. Benchmark1S 5000000 334 ns/op
  119. Benchmark1F 5000000 302 ns/op
  120. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement