Last active
June 30, 2023 17:24
-
-
Save mroth/b98a43537f40e8cd47384fb95d6c1bde to your computer and use it in GitHub Desktop.
buffered randomness throughput benchmarks
This file contains hidden or 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 ( | |
"bufio" | |
"crypto/rand" | |
"strconv" | |
"testing" | |
) | |
func BenchmarkRandReads(b *testing.B) { | |
const size = 16 // assume we need 16 bytes of entropy per seed operation | |
var dst [size]byte | |
b.Run("normal", func(b *testing.B) { | |
b.SetBytes(size) | |
for i := 0; i < b.N; i++ { | |
_, _ = rand.Read(dst[:]) | |
} | |
}) | |
b.Run("buffered", func(b *testing.B) { | |
bufSizes := []int{128, 256, 512, 1024, 2048, 4096} | |
for _, bs := range bufSizes { | |
b.Run(strconv.Itoa(bs), func(b *testing.B) { | |
b.SetBytes(size) | |
bufReader := bufio.NewReaderSize(rand.Reader, bs) | |
for i := 0; i < b.N; i++ { | |
_, _ = bufReader.Read(dst[:]) | |
} | |
}) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on laptop: