Advertisement
Guest User

Untitled

a guest
Jan 11th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.41 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "testing"
  7. )
  8.  
  9. type I struct {
  10.     A string
  11.     B int
  12.     M map[string]string
  13. }
  14.  
  15. type T struct {
  16.     A string
  17.     B int
  18.     M map[string]string
  19.     I interface{}
  20. }
  21.  
  22. var d = createT()
  23.  
  24. func BenchmarkJsonMarshal(b *testing.B) {
  25.  
  26.     for n := 0; n < b.N; n++ {
  27.         b, _ := json.Marshal(d)
  28.         b = b
  29.     }
  30.  
  31. }
  32.  
  33. func BenchmarkStreamPrintPlusV(b *testing.B) {
  34.  
  35.     for n := 0; n < b.N; n++ {
  36.         b := fmt.Sprintf("%+v", d)
  37.         b = b
  38.     }
  39. }
  40. func BenchmarkStreamPrintHashV(b *testing.B) {
  41.  
  42.     for n := 0; n < b.N; n++ {
  43.         b := fmt.Sprintf("%#v", d)
  44.         b = b
  45.     }
  46. }
  47. func BenchmarkStreamPrintPureV(b *testing.B) {
  48.  
  49.     for n := 0; n < b.N; n++ {
  50.         b := fmt.Sprintf("%v", d)
  51.         b = b
  52.     }
  53. }
  54.  
  55. func createT() T {
  56.  
  57.     s := I{
  58.         A: "string",
  59.         B: 42,
  60.         M: map[string]string{
  61.             "Key1": "Value1",
  62.             "Key2": "Value2",
  63.         },
  64.     }
  65.  
  66.     return T{
  67.         A: "string",
  68.         B: 42,
  69.         M: map[string]string{
  70.             "Key1": "Value1",
  71.             "Key2": "Value2",
  72.         },
  73.         I: s,
  74.     }
  75. }
  76.  
  77. /*
  78.  
  79.  go test -benchmem -bench=.
  80. BenchmarkJsonMarshal-4            500000          3095 ns/op        1216 B/op         25 allocs/op
  81. BenchmarkStreamPrintPlusV-4       500000          2677 ns/op         616 B/op         21 allocs/op
  82. BenchmarkStreamPrintHashV-4       500000          3546 ns/op         696 B/op         21 allocs/op
  83. BenchmarkStreamPrintPureV-4      1000000          2223 ns/op         560 B/op         14 allocs/op
  84. PASS
  85. ok      _/home/adam/Go/src  7.009s
  86.  
  87. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement