From aa49b3700cc8775e696b14c1e95b1b4e6bab8aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Senart?= Date: Fri, 11 Oct 2013 13:57:07 +0200 Subject: [PATCH] Use encoding/hex for generating UID strings BenchmarkFmtString 5000000 711 ns/op BenchmarkHexString 10000000 171 ns/op --- src/http/uid.go | 4 ++-- src/http/uid_test.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/http/uid_test.go diff --git a/src/http/uid.go b/src/http/uid.go index da3a468..45e5898 100644 --- a/src/http/uid.go +++ b/src/http/uid.go @@ -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) } diff --git a/src/http/uid_test.go b/src/http/uid_test.go new file mode 100644 index 0000000..3fa2061 --- /dev/null +++ b/src/http/uid_test.go @@ -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) + } +}