• Stars
    star
    192
  • Rank 202,019 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast and scalable pseudorandom generator for Go

Build Status GoDoc Go Report

fastrand

Fast pseudorandom number generator.

Features

  • Optimized for speed.
  • Performance scales on multiple CPUs.

How does it work?

It abuses sync.Pool for maintaining "per-CPU" pseudorandom number generators.

TODO: firgure out how to use real per-CPU pseudorandom number generators.

Benchmark results

$ GOMAXPROCS=1 go test -bench=. github.com/valyala/fastrand
goos: linux
goarch: amd64
pkg: github.com/valyala/fastrand
BenchmarkUint32n                   	50000000	        29.7 ns/op
BenchmarkRNGUint32n                	200000000	         6.50 ns/op
BenchmarkRNGUint32nWithLock        	100000000	        21.5 ns/op
BenchmarkMathRandInt31n            	50000000	        31.8 ns/op
BenchmarkMathRandRNGInt31n         	100000000	        17.9 ns/op
BenchmarkMathRandRNGInt31nWithLock 	50000000	        30.2 ns/op
PASS
ok  	github.com/valyala/fastrand	10.634s
$ GOMAXPROCS=2 go test -bench=. github.com/valyala/fastrand
goos: linux
goarch: amd64
pkg: github.com/valyala/fastrand
BenchmarkUint32n-2                     	100000000	        17.6 ns/op
BenchmarkRNGUint32n-2                  	500000000	         3.36 ns/op
BenchmarkRNGUint32nWithLock-2          	50000000	        32.0 ns/op
BenchmarkMathRandInt31n-2              	20000000	        51.2 ns/op
BenchmarkMathRandRNGInt31n-2           	100000000	        11.0 ns/op
BenchmarkMathRandRNGInt31nWithLock-2   	20000000	        91.0 ns/op
PASS
ok  	github.com/valyala/fastrand	9.543s
$ GOMAXPROCS=4 go test -bench=. github.com/valyala/fastrand
goos: linux
goarch: amd64
pkg: github.com/valyala/fastrand
BenchmarkUint32n-4                     	100000000	        14.2 ns/op
BenchmarkRNGUint32n-4                  	500000000	         3.30 ns/op
BenchmarkRNGUint32nWithLock-4          	20000000	        88.7 ns/op
BenchmarkMathRandInt31n-4              	10000000	       145 ns/op
BenchmarkMathRandRNGInt31n-4           	200000000	         8.35 ns/op
BenchmarkMathRandRNGInt31nWithLock-4   	20000000	       102 ns/op
PASS
ok  	github.com/valyala/fastrand	11.534s

As you can see, fastrand.Uint32n scales on multiple CPUs, while rand.Int31n doesn't scale. Their performance is comparable on GOMAXPROCS=1, but fastrand.Uint32n runs 3x faster than rand.Int31n on GOMAXPROCS=2 and 10x faster than rand.Int31n on GOMAXPROCS=4.

More Repositories

1

fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
Go
21,773
star
2

quicktemplate

Fast, powerful, yet easy to use template engine for Go. Optimized for speed, zero memory allocations in hot paths. Up to 20x faster than html/template
Go
2,967
star
3

fastjson

Fast JSON parser and validator for Go. No custom structs, no code generation, no reflection
Go
2,104
star
4

bytebufferpool

Anti-memory-waste byte buffer pool
Go
1,082
star
5

fasttemplate

Simple and fast template engine for Go
Go
791
star
6

gorpc

Simple, fast and scalable golang rpc library for high load
Go
685
star
7

httpteleport

Transfer 10Gbps http traffic over 1Gbps networks :)
Go
455
star
8

gozstd

go wrapper for zstd
C
405
star
9

ybc

Fast in-process BLOB cache with persistence support
C
395
star
10

goloris

Slowloris for nginx DoS. Written in go
Go
352
star
11

tcplisten

Customizable TCP net.Listener for Go
Go
142
star
12

gheap

Fast generalized heap tree algorithms in C++ and C. Provides simultaneous support for D-heap and B-heap.
C++
128
star
13

fastrpc

Building blocks for fast rpc systems
Go
83
star
14

tsvreader

Fast reader for TSV streams
Go
62
star
15

chclient

Fast http client for SELECT queries in clickhouse
Go
46
star
16

histogram

Fast histograms for Go
Go
30
star
17

suggester

Suggester - the heart for full-text auto-complete web services
Python
29
star
18

swift-response

Go response to `swift vs node.js benchmarks` :)
Go
19
star
19

batcher

Go package for grouping items in batches
Go
18
star
20

hpajaxrpc

Lightweight RPC library for high-performance AJAX applications
JavaScript
9
star
21

go-launcher

Launcher for Go services (and other executables) accepting over9000 command-line flags
Go
6
star
22

big_int

Arbitrary precision math implementation
C
6
star
23

multiplexing-rpc

Cross-platform RPC library supporting multiplexed and parallel RPC over a single byte stream
C
6
star
24

simple-critbit

Simple implementation of a crit-bit tree in C
C
5
star
25

fiber-framework

Cross-platform framework for userspace threads aka fibers
C
5
star
26

image-resizer-imagemagick

Go
4
star
27

gobcodec

Bytes-oriented codec on top of gob encoding
Go
4
star
28

geocache

A prototype of O(1) nearest dynamic points' locator API in 3D space
Python
2
star
29

image-resizer

Go
2
star