Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package test
import (
"crypto/rand"
"encoding/binary"
"testing"
"unsafe"
)
func BenchmarkCryptoRandRead(b *testing.B) {
b.SetBytes(8)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var seed int64
if err := binary.Read(rand.Reader, binary.LittleEndian, &seed); err != nil {
panic(err)
}
_ = seed
}
}
func BenchmarkCryptoRandReadUnsafe(b *testing.B) {
b.SetBytes(8)
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf [unsafe.Sizeof(int64(0))]byte
if _, err := rand.Read(buf[:]); err != nil {
panic(err)
}
seed := (*int64)(unsafe.Pointer(&buf))
_ = seed
}
}
@mono0x

This comment has been minimized.

Copy link
Owner Author

commented Feb 26, 2018

goos: darwin
goarch: amd64
BenchmarkCryptoRandRead-8         	 2000000	       665 ns/op	  12.02 MB/s
BenchmarkCryptoRandReadUnsafe-8   	 2000000	       647 ns/op	  12.35 MB/s
PASS
ok  	_/tmp	3.959s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.