Advertisement
Guest User

concat strings and bytes

a guest
Dec 14th, 2015
4,564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. AMD Athlon64 X2
  4. 2GB DDR2
  5.  
  6. Go 1.5.1 linux/amd64
  7.  
  8. Ubuntu 15.10
  9. Linux asus 4.2.0-19-generic #23-Ubuntu SMP Wed Nov 11 11:39:30 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  10.  
  11. BenchmarkStringSprintf-2         100      10140559 ns/op     5518024 B/op       4616 allocs/op
  12. BenchmarkStringJoin-2            200       6962496 ns/op     5388737 B/op       2000 allocs/op
  13. BenchmarkStringAdd-2             500       3440360 ns/op     2694352 B/op        999 allocs/op
  14. BenchmarkStringWrite-2         30000         54597 ns/op       18656 B/op          8 allocs/op
  15. BenchmarkStringAppend-2        30000         41325 ns/op       20736 B/op         13 allocs/op
  16. BenchmarkBytesJoin-2             300       4883126 ns/op     2694449 B/op       1001 allocs/op
  17. BenchmarkBytesAppend-2         30000         53491 ns/op       20736 B/op         13 allocs/op
  18. BenchmarkBytesWrite-2          20000         64233 ns/op       18656 B/op          8 allocs/op
  19.  
  20. Feel free to change the LIMIT.
  21.  
  22. */
  23. package test
  24.  
  25. import (
  26.     "bytes"
  27.     "fmt"
  28.     "strings"
  29.     "testing"
  30. )
  31.  
  32. var (
  33.     s1 = "local"
  34.     b1 = []byte("local")
  35. )
  36.  
  37. const LIMIT = 1000
  38.  
  39. func BenchmarkStringSprintf(b *testing.B) {
  40.     var q string
  41.     for i := 0; i < b.N; i++ {
  42.         for j := 0; j < LIMIT; j++ {
  43.             q = fmt.Sprintf("%s%s", q, s1)
  44.         }
  45.         q = ""
  46.     }
  47.     b.ReportAllocs()
  48. }
  49.  
  50. func BenchmarkStringJoin(b *testing.B) {
  51.     var q string
  52.     for i := 0; i < b.N; i++ {
  53.         for j := 0; j < LIMIT; j++ {
  54.             q = strings.Join([]string{q, s1}, "")
  55.         }
  56.         q = ""
  57.     }
  58.     b.ReportAllocs()
  59. }
  60.  
  61. func BenchmarkStringAdd(b *testing.B) {
  62.     var q string
  63.     for i := 0; i < b.N; i++ {
  64.         for j := 0; j < LIMIT; j++ {
  65.             q = q + s1
  66.         }
  67.         q = ""
  68.     }
  69.     b.ReportAllocs()
  70. }
  71.  
  72. func BenchmarkStringWrite(b *testing.B) {
  73.     q := new(bytes.Buffer)
  74.     for i := 0; i < b.N; i++ {
  75.         for j := 0; j < LIMIT; j++ {
  76.             q.WriteString(s1)
  77.         }
  78.         q = new(bytes.Buffer)
  79.     }
  80. }
  81.  
  82. func BenchmarkStringAppend(b *testing.B) {
  83.     var q []byte
  84.     for i := 0; i < b.N; i++ {
  85.         for j := 0; j < LIMIT; j++ {
  86.             q = append(q, s1...)
  87.         }
  88.         q = nil
  89.     }
  90.     b.ReportAllocs()
  91. }
  92.  
  93. func BenchmarkBytesJoin(b *testing.B) {
  94.     var q []byte
  95.     for i := 0; i < b.N; i++ {
  96.         for j := 0; j < LIMIT; j++ {
  97.             q = bytes.Join([][]byte{q, b1}, nil)
  98.         }
  99.         q = nil
  100.     }
  101.     b.ReportAllocs()
  102. }
  103.  
  104. func BenchmarkBytesAppend(b *testing.B) {
  105.     var q []byte
  106.     for i := 0; i < b.N; i++ {
  107.         for j := 0; j < LIMIT; j++ {
  108.             q = append(q, b1...)
  109.         }
  110.         q = nil
  111.     }
  112.     b.ReportAllocs()
  113. }
  114.  
  115. func BenchmarkBytesWrite(b *testing.B) {
  116.     q := new(bytes.Buffer)
  117.     for i := 0; i < b.N; i++ {
  118.         for j := 0; j < LIMIT; j++ {
  119.             q.Write(b1)
  120.         }
  121.         q = new(bytes.Buffer)
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement