Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "testing"
  5. )
  6.  
  7. func BenchmarkTestForeach(b *testing.B) {
  8. b.Run("slice", func(b *testing.B) {
  9. dataS := []string{
  10. "aaaaaaaa1111",
  11. "aaaaaaaa2222",
  12. "aaaaaaaa9999",
  13. "aaaaaaaa4444",
  14. "aaaaaaaa5555",
  15. "aaaaaaaa6666",
  16. "aaaaaaaa7777",
  17. "aaaaaaaa8888",
  18. "aaaaaaaa9999",
  19. }
  20.  
  21. for i := 0; i < b.N; i++ {
  22. for _, v := range dataS {
  23. if v == "aaaaaaaa9999" {
  24. break
  25. }
  26. }
  27. }
  28. })
  29.  
  30. b.Run("map", func(b *testing.B) {
  31. dataM := map[string]bool{
  32. "aaaaaaaa1111": true,
  33. "aaaaaaaa2222": true,
  34. "aaaaaaaa3333": true,
  35. "aaaaaaaa4444": true,
  36. "aaaaaaaa5555": true,
  37. "aaaaaaaa6666": true,
  38. "aaaaaaaa7777": true,
  39. "aaaaaaaa8888": true,
  40. "aaaaaaaa9999": true,
  41. }
  42.  
  43. for i := 0; i < b.N; i++ {
  44. v, ok := dataM["aaaaaaaa9999"]
  45. if ok && v {
  46. continue
  47. }
  48. }
  49.  
  50. })
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement