Advertisement
Guest User

Untitled

a guest
Mar 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.01 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "testing"
  5.  
  6.     "github.com/Vivino/go-api/app/helpers"
  7. )
  8.  
  9. func BenchmarkSetSlice(b *testing.B) {
  10.     s := helpers.NewSet32()
  11.     for n := 0; n < b.N; n++ {
  12.         s.Add(uint32(n))
  13.     }
  14.  
  15.     foo := s.Slice()
  16.     foo = foo
  17. }
  18.  
  19. func BenchmarkHelperNativeSlicePreAlloc(b *testing.B) {
  20.     s := make([]int, b.N)
  21.     for n := 0; n < b.N; n++ {
  22.         s[n] = n
  23.     }
  24. }
  25.  
  26. func BenchmarkHelperNativeSliceNoPreAlloc(b *testing.B) {
  27.     var s []int
  28.     for n := 0; n < b.N; n++ {
  29.         s = append(s, n)
  30.     }
  31. }
  32.  
  33. /*
  34. $ go test -bench=. slice_test.go -benchmem=true
  35. 2018/03/12 14:15:19 DEBUG Now using Go's stdlib log package (via loggers/mappers/stdlib).
  36. goos: linux
  37. goarch: amd64
  38. BenchmarkSetSlice-4                         10000000       274 ns/op         29 B/op       0 allocs/op
  39. BenchmarkHelperNativeSlicePreAlloc-4        1000000000       3.16 ns/op       8 B/op       0 allocs/op
  40. BenchmarkHelperNativeSliceNoPreAlloc-4      300000000        4.52 ns/op      40 B/op       0 allocs/op
  41. PASS
  42. ok      command-line-argumentsfalse8.625s
  43. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement