Commit f7467e85 authored by Robert Griesemer's avatar Robert Griesemer

encoding/binary: added benchmarks

binary.BenchmarkPutUvarint32 20000000	 85.6 ns/op
binary.BenchmarkPutUvarint64 10000000	299   ns/op

R=rsc
CC=golang-dev
https://golang.org/cl/5148049
parent 5d51b0ae
...@@ -11,8 +11,8 @@ import ( ...@@ -11,8 +11,8 @@ import (
) )
func testConstant(t *testing.T, w uint, max int) { func testConstant(t *testing.T, w uint, max int) {
var buf [MaxVarintLen64]byte buf := make([]byte, MaxVarintLen64)
n := PutUvarint(buf[:], 1<<w-1) n := PutUvarint(buf, 1<<w-1)
if n != max { if n != max {
t.Errorf("MaxVarintLen%d = %d; want %d", w, max, n) t.Errorf("MaxVarintLen%d = %d; want %d", w, max, n)
} }
...@@ -25,7 +25,7 @@ func TestConstants(t *testing.T) { ...@@ -25,7 +25,7 @@ func TestConstants(t *testing.T) {
} }
func testVarint(t *testing.T, x int64) { func testVarint(t *testing.T, x int64) {
var buf1 [10]byte buf1 := make([]byte, MaxVarintLen64)
n := PutVarint(buf1[:], x) n := PutVarint(buf1[:], x)
y, m := Varint(buf1[0:n]) y, m := Varint(buf1[0:n])
if x != y { if x != y {
...@@ -53,7 +53,7 @@ func testVarint(t *testing.T, x int64) { ...@@ -53,7 +53,7 @@ func testVarint(t *testing.T, x int64) {
} }
func testUvarint(t *testing.T, x uint64) { func testUvarint(t *testing.T, x uint64) {
var buf1 [10]byte buf1 := make([]byte, MaxVarintLen64)
n := PutUvarint(buf1[:], x) n := PutUvarint(buf1[:], x)
y, m := Uvarint(buf1[0:n]) y, m := Uvarint(buf1[0:n])
if x != y { if x != y {
...@@ -162,3 +162,21 @@ func TestNonCanonicalZero(t *testing.T) { ...@@ -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))
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment