Generate 8 bytes (64 bit) PRNG identifiers blazingly fast
Spoiler alert: +970.22% CPU and zero allocations
import "github.com/zerjioang/prng"
Example program that creates a PRNG UUID of 64 bits encoded as hex string
package main
import (
"fmt"
"github.com/zerjioang/prng"
)
func main(){
uuid := prng.New()
fmt.Println(uuid)
}
Using standard Go package
func gen() string {
dst := make([]byte, 8)
for i:=0; i<8;i++ {
dst[i] = byte(rand.Intn(256))
}
return hex.EncodeToString(dst[:])
}
// Output: 336159b54b9e2839
Using fastprng
package
func gen() string {
return prng.New()
}
// Output: dca90c93b8848229
Note: always run your own performance test on your hardware
goos: linux
goarch: amd64
cpu: Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
BenchmarkRng-0-12 41574139 56.28 ns/op 17.77 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 43105999 55.83 ns/op 17.91 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 42470292 55.33 ns/op 18.07 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 44294082 55.39 ns/op 18.05 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 43065757 55.16 ns/op 18.13 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 43489963 54.92 ns/op 18.21 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 40353843 55.29 ns/op 18.09 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 44173886 55.38 ns/op 18.06 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 44519041 54.78 ns/op 18.26 MB/s 0 B/op 0 allocs/op
BenchmarkRng-0-12 43383703 55.69 ns/op 17.96 MB/s 0 B/op 0 allocs/op
Spoiler alert: +970.22% CPU and zero allocations
go test -v -run=^$ -bench=^BenchmarkRng/\$ -benchtime=2s -count=10 > old.txt
Make some changes
go test -v -run=^$ -bench=^BenchmarkRng/\$ -benchtime=2s -count=10 > new.txt
And then, compare results with benchstat
benstat old.txt new.txt
name old time/op new time/op delta
Rng/uuid-0-12 593ns ± 2% 55ns ± 2% -90.66% (p=0.000 n=9+10)
name old speed new speed delta
Rng/uuid-0-12 1.69MB/s ± 2% 18.05MB/s ± 2% +970.22% (p=0.000 n=9+10)
name old alloc/op new alloc/op delta
Rng/uuid-0-12 32.0B ± 0% 0.0B -100.00% (p=0.000 n=10+10)
name old allocs/op new allocs/op delta
Rng/uuid-0-12 2.00 ± 0% 0.00 -100.00% (p=0.000 n=10+10)