Commit 57370a87 authored by Alberto Donizetti's avatar Alberto Donizetti Committed by Brad Fitzpatrick

encoding/hex: change lookup table from string to array

name            old time/op  new time/op  delta
Encode/256-4     431ns ± 2%   391ns ± 2%   -9.36%  (p=0.000 n=8+8)
Encode/1024-4   1.68µs ± 0%  1.51µs ± 0%   -9.91%  (p=0.001 n=7+7)
Encode/4096-4   6.68µs ± 0%  6.03µs ± 1%   -9.69%  (p=0.000 n=8+8)
Encode/16384-4  27.0µs ± 1%  24.0µs ± 0%  -11.03%  (p=0.000 n=8+7)

Change-Id: I6994e02f77797349c4e188377d84f97dffe98399
Reviewed-on: https://go-review.googlesource.com/27254Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 17eee310
......@@ -12,7 +12,10 @@ import (
"io"
)
const hextable = "0123456789abcdef"
var hextable = [16]byte{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f',
}
// EncodedLen returns the length of an encoding of n source bytes.
func EncodedLen(n int) int { return n * 2 }
......
......@@ -6,6 +6,7 @@ package hex
import (
"bytes"
"fmt"
"testing"
)
......@@ -151,3 +152,18 @@ var expectedHexDump = []byte(`00000000 1e 1f 20 21 22 23 24 25 26 27 28 29 2a
00000010 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d |./0123456789:;<=|
00000020 3e 3f 40 41 42 43 44 45 |>?@ABCDE|
`)
var sink []byte
func BenchmarkEncode(b *testing.B) {
for _, size := range []int{256, 1024, 4096, 16384} {
src := bytes.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size/8)
sink = make([]byte, 2*size)
b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
Encode(sink, src)
}
})
}
}
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