Skip to content

Instantly share code, notes, and snippets.

@Zhang-Siyang
Last active January 11, 2023 15:32
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 Zhang-Siyang/cb10162e8f98e87041201d15aea89088 to your computer and use it in GitHub Desktop.
Save Zhang-Siyang/cb10162e8f98e87041201d15aea89088 to your computer and use it in GitHub Desktop.
package main
// go test -test.bench '.*'
// goos: darwin
// goarch: arm64
// BenchmarkRandRead4KB-8 356580 3335 ns/op
// BenchmarkCryptoRead4KB-8 158610 7540 ns/op
import (
rand2 "crypto/rand"
"math/rand"
"testing"
"time"
)
var r = make([]byte, 4096)
func RandRead4KB() {
rand.Read(r)
_ = r
}
func CryptoRead4KB() {
rand2.Read(r)
_ = r
}
func BenchmarkRandRead4KB(b *testing.B) {
rand.Seed(time.Now().UnixNano())
b.ResetTimer()
for i := 0; i < b.N; i++ {
RandRead4KB()
}
}
func BenchmarkCryptoRead4KB(b *testing.B) {
for i := 0; i < b.N; i++ {
CryptoRead4KB()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment