Use encoding/hex for generating UID strings

BenchmarkFmtString   5000000         711 ns/op
BenchmarkHexString  10000000         171 ns/op
This commit is contained in:
Tomás Senart 2013-10-11 13:57:07 +02:00
parent 37c4d72ff7
commit aa49b3700c
2 changed files with 23 additions and 2 deletions

View File

@ -2,7 +2,7 @@ package http
import ( import (
"crypto/rand" "crypto/rand"
"fmt" "encoding/hex"
"io" "io"
) )
@ -20,5 +20,5 @@ func uid() string {
panic(err) panic(err)
} }
return fmt.Sprintf("%x", id) return hex.EncodeToString(id)
} }

21
src/http/uid_test.go Normal file
View File

@ -0,0 +1,21 @@
package http
import (
"encoding/hex"
"fmt"
"testing"
)
func BenchmarkFmtString(b *testing.B) {
id := []byte("1234567891234567")
for i := 0; i < b.N; i++ {
fmt.Sprintf("%x", id)
}
}
func BenchmarkHexString(b *testing.B) {
id := []byte("1234567891234567")
for i := 0; i < b.N; i++ {
hex.EncodeToString(id)
}
}