Created
February 26, 2018 05:58
-
-
Save mono0x/cb63061bdb89d158e411e5215f41dd2e 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 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 | |
} | |
} |
Author
mono0x
commented
Feb 26, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment