Skip to content
/ prng Public

A pure Go Blazinly fast PRNG 64 bit ID generator (normal distributed)

Notifications You must be signed in to change notification settings

zerjioang/prng

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blazinly fast PRNG (normal distributed)

Generate 8 bytes (64 bit) PRNG identifiers blazingly fast

Spoiler alert: +970.22% CPU and zero allocations

Usage

import "github.com/zerjioang/prng"

Example Program

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)
}

Performance

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

Performance Results

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

Speedup calculation

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)

About

A pure Go Blazinly fast PRNG 64 bit ID generator (normal distributed)

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages