neo-go/pkg/crypto/hash/merkle_bench_test.go
Roman Khimov d52e79668b hash: introduce memory-optimized merkle root hash calculation routine
NewMerkleTree is a memory hog, we can do better than that:

BenchmarkMerkle/NewMerkleTree-8                       13          88434670 ns/op        20828207 B/op     300035 allocs/op
BenchmarkMerkle/CalcMerkleRoot-8                      15          69264150 ns/op               0 B/op          0 allocs/op
2020-09-15 18:38:15 +03:00

37 lines
739 B
Go

package hash
import (
"math/rand"
"testing"
"time"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require"
)
func BenchmarkMerkle(t *testing.B) {
var err error
var hashes = make([]util.Uint256, 100000)
var h = make([]byte, 32)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range hashes {
r.Read(h)
hashes[i], err = util.Uint256DecodeBytesBE(h)
require.NoError(t, err)
}
t.Run("NewMerkleTree", func(t *testing.B) {
t.ResetTimer()
for n := 0; n < t.N; n++ {
tr, err := NewMerkleTree(hashes)
require.NoError(t, err)
_ = tr.Root()
}
})
t.Run("CalcMerkleRoot", func(t *testing.B) {
t.ResetTimer()
for n := 0; n < t.N; n++ {
_ = CalcMerkleRoot(hashes)
}
})
}