-
-
Save holiman/c556dabf3029924a454757c138841b3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "testing" | |
type Address [20]byte | |
type AccountRef Address | |
func (ar AccountRef) Address() Address { return (Address)(ar) } | |
type ContractRef interface { | |
Address() Address | |
} | |
type Contract struct { | |
CallerAddress Address | |
CodeAddr *Address | |
} | |
func BadOperation(caller ContractRef, addr Address) { | |
if ok := true; ok { | |
return | |
} else { | |
contract := &Contract{CallerAddress: caller.Address()} | |
contract.CodeAddr = &addr | |
} | |
return | |
} | |
func GoodOperation(caller ContractRef, addr Address) { | |
if ok := true; ok { | |
return | |
} else { | |
//contract := &Contract{CallerAddress: caller.Address()} | |
//contract.CodeAddr = &addr | |
} | |
return | |
} | |
func BadAlloc() { | |
for i := uint64(0); i < 100000; i++ { | |
BadOperation(AccountRef(Address{}), Address{}) | |
} | |
} | |
func GoodAlloc() { | |
for i := uint64(0); i < 100000; i++ { | |
GoodOperation(AccountRef(Address{}), Address{}) | |
} | |
} | |
func BenchmarkAllocs(b *testing.B) { | |
b.Run("bad", func(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
BadAlloc() | |
} | |
}) | |
b.Run("good", func(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
GoodAlloc() | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
goos: linux | |
goarch: amd64 | |
pkg: github.com/ethereum/go-ethereum/cmd/testvm | |
BenchmarkAllocs | |
BenchmarkAllocs/bad | |
BenchmarkAllocs/bad-6 100 10296649 ns/op 6400030 B/op 200000 allocs/op | |
BenchmarkAllocs/good | |
BenchmarkAllocs/good-6 34618 29632 ns/op 0 B/op 0 allocs/op | |
PASS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[user@work testvm]$ go version
go version go1.14.4 linux/amd64