From 143b56b5ac264cfd3fbc7e50f0373383808c45c6 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 8 Nov 2017 22:40:11 +0530 Subject: [PATCH] Use hex.EncodeToString to encode to hex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using the EncodeToString function from the encoding/hex package is much faster than calling the fmt.Sprintf with %x Benchmark results below with the following code func BenchmarkHexPrint(b *testing.B) { data := []byte("hellothere") for n := 0; n < b.N; n++ { // _ = fmt.Sprintf("%x", data) _ = hex.EncodeToString(data) } } name old time/op new time/op delta HexPrint-4 188ns ± 1% 99ns ± 1% -47.40% (p=0.008 n=5+5) name old alloc/op new alloc/op delta HexPrint-4 64.0B ± 0% 64.0B ± 0% ~ (all equal) name old allocs/op new allocs/op delta HexPrint-4 2.00 ± 0% 2.00 ± 0% ~ (all equal) --- pkg/util/encoding.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/util/encoding.go b/pkg/util/encoding.go index e87da9d3d55..0edb721e422 100644 --- a/pkg/util/encoding.go +++ b/pkg/util/encoding.go @@ -8,7 +8,6 @@ import ( "encoding/base64" "encoding/hex" "errors" - "fmt" "hash" "strings" ) @@ -30,7 +29,7 @@ func GetRandomString(n int, alphabets ...byte) string { func EncodePassword(password string, salt string) string { newPasswd := PBKDF2([]byte(password), []byte(salt), 10000, 50, sha256.New) - return fmt.Sprintf("%x", newPasswd) + return hex.EncodeToString(newPasswd) } // Encode string to md5 hex value.