Hi, I want to calculate hash on 3 strings. First string is always the same, the other may vary. The first approach is to calculate the hash each time for the 3 strings ( BenchmarkHash) Another approach would be to calculate once for the first string, and then reuse this hash to calculate the hash with the other 2 strings (BenchmarkCopyHash) The difficulty is that sha256.New() returns a pointer, we have to copy the first hash. To do this, I created the function copyHash() But the performances are not exceptional.
Do you have another idea to do this in efficient way? BenchmarkHash-8 1000000 1761 ns/op 176 B/op 4 allocs/op BenchmarkCopyHash-8 1000000 1519 ns/op 240 B/op 4 allocs/op var m1 = strings.Repeat("a", 64) var m2 = strings.Repeat("b", 48) var m3 = strings.Repeat("c", 32) func BenchmarkHash(b *testing.B) { var ( d hash.Hash ) d = sha256.New() for n := 0; n < b.N; n++ { d.Reset() d.Write([]byte(m1)) d.Write([]byte(m2)) d.Write([]byte(m3)) d.Sum(nil) } } func BenchmarkCopyHash(b *testing.B) { var ( d1 hash.Hash d2 hash.Hash ) d1 = sha256.New() d1.Write([]byte(m1)) for n := 0; n < b.N; n++ { d2 = copyHash(d1) d2.Write([]byte(m2)) d2.Write([]byte(m3)) d2.Sum(nil) } } func copyHash(src hash.Hash) hash.Hash { typ := reflect.TypeOf(src).Elem() val := reflect.ValueOf(src).Elem() elem := reflect.New(typ).Elem() elem.Set(val) return elem.Addr().Interface().(hash.Hash) } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.