]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: added benchmarks
authorRobert Griesemer <gri@golang.org>
Wed, 5 Oct 2011 20:04:43 +0000 (13:04 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 5 Oct 2011 20:04:43 +0000 (13:04 -0700)
binary.BenchmarkPutUvarint32 20000000  85.6 ns/op
binary.BenchmarkPutUvarint64 10000000 299   ns/op

R=rsc
CC=golang-dev
https://golang.org/cl/5148049

src/pkg/encoding/binary/varint_test.go

index 1ceb4cd4b1257e40ad727e034f16e1065cd2fc0e..ef51f09293b6d33614c11d15d825045fd9bc99c5 100644 (file)
@@ -11,8 +11,8 @@ import (
 )
 
 func testConstant(t *testing.T, w uint, max int) {
-       var buf [MaxVarintLen64]byte
-       n := PutUvarint(buf[:], 1<<w-1)
+       buf := make([]byte, MaxVarintLen64)
+       n := PutUvarint(buf, 1<<w-1)
        if n != max {
                t.Errorf("MaxVarintLen%d = %d; want %d", w, max, n)
        }
@@ -25,7 +25,7 @@ func TestConstants(t *testing.T) {
 }
 
 func testVarint(t *testing.T, x int64) {
-       var buf1 [10]byte
+       buf1 := make([]byte, MaxVarintLen64)
        n := PutVarint(buf1[:], x)
        y, m := Varint(buf1[0:n])
        if x != y {
@@ -53,7 +53,7 @@ func testVarint(t *testing.T, x int64) {
 }
 
 func testUvarint(t *testing.T, x uint64) {
-       var buf1 [10]byte
+       buf1 := make([]byte, MaxVarintLen64)
        n := PutUvarint(buf1[:], x)
        y, m := Uvarint(buf1[0:n])
        if x != y {
@@ -162,3 +162,21 @@ func TestNonCanonicalZero(t *testing.T) {
 
        }
 }
+
+func BenchmarkPutUvarint32(b *testing.B) {
+       buf := make([]byte, MaxVarintLen32)
+       for i := 0; i < b.N; i++ {
+               for j := uint(0); j < MaxVarintLen32; j++ {
+                       PutUvarint(buf, 1<<(j*7))
+               }
+       }
+}
+
+func BenchmarkPutUvarint64(b *testing.B) {
+       buf := make([]byte, MaxVarintLen64)
+       for i := 0; i < b.N; i++ {
+               for j := uint(0); j < MaxVarintLen64; j++ {
+                       PutUvarint(buf, 1<<(j*7))
+               }
+       }
+}