Use encoding/hex for generating UID strings
BenchmarkFmtString 5000000 711 ns/op BenchmarkHexString 10000000 171 ns/op
This commit is contained in:
parent
37c4d72ff7
commit
aa49b3700c
|
@ -2,7 +2,7 @@ package http
|
|||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
)
|
||||
|
||||
|
@ -20,5 +20,5 @@ func uid() string {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%x", id)
|
||||
return hex.EncodeToString(id)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue