Skip to content

Instantly share code, notes, and snippets.

@mono0x
Created February 26, 2018 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0x/cb63061bdb89d158e411e5215f41dd2e to your computer and use it in GitHub Desktop.
Save mono0x/cb63061bdb89d158e411e5215f41dd2e to your computer and use it in GitHub Desktop.
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
Copy link
Author

mono0x 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