source: code/trunk/vendor/lukechampine.com/uint128/README.md@ 822

Last change on this file since 822 was 822, checked in by yakumo.izuru, 22 months ago

Prefer immortal.run over runit and rc.d, use vendored modules
for convenience.

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 2.4 KB
Line 
1uint128
2-------
3
4[![GoDoc](https://godoc.org/github.com/lukechampine/uint128?status.svg)](https://godoc.org/github.com/lukechampine/uint128)
5[![Go Report Card](http://goreportcard.com/badge/github.com/lukechampine/uint128)](https://goreportcard.com/report/github.com/lukechampine/uint128)
6
7```
8go get lukechampine.com/uint128
9```
10
11`uint128` provides a high-performance `Uint128` type that supports standard arithmetic
12operations. Unlike `math/big`, operations on `Uint128` values always produce new values
13instead of modifying a pointer receiver. A `Uint128` value is therefore immutable, just
14like `uint64` and friends.
15
16The name `uint128.Uint128` stutters, so I recommend either using a "dot import"
17or aliasing `uint128.Uint128` to give it a project-specific name. Embedding the type
18is not recommended, because methods will still return `uint128.Uint128`; this means that,
19if you want to extend the type with new methods, your best bet is probably to copy the
20source code wholesale and rename the identifier. ¯\\\_(ツ)\_/¯
21
22
23# Benchmarks
24
25Addition, multiplication, and subtraction are on par with their native 64-bit
26equivalents. Division is slower: ~20x slower when dividing a `Uint128` by a
27`uint64`, and ~100x slower when dividing by a `Uint128`. However, division is
28still faster than with `big.Int` (for the same operands), especially when
29dividing by a `uint64`.
30
31```
32BenchmarkArithmetic/Add-4 2000000000 0.45 ns/op 0 B/op 0 allocs/op
33BenchmarkArithmetic/Sub-4 2000000000 0.67 ns/op 0 B/op 0 allocs/op
34BenchmarkArithmetic/Mul-4 2000000000 0.42 ns/op 0 B/op 0 allocs/op
35BenchmarkArithmetic/Lsh-4 2000000000 1.06 ns/op 0 B/op 0 allocs/op
36BenchmarkArithmetic/Rsh-4 2000000000 1.06 ns/op 0 B/op 0 allocs/op
37
38BenchmarkDivision/native_64/64-4 2000000000 0.39 ns/op 0 B/op 0 allocs/op
39BenchmarkDivision/Div_128/64-4 2000000000 6.28 ns/op 0 B/op 0 allocs/op
40BenchmarkDivision/Div_128/128-4 30000000 45.2 ns/op 0 B/op 0 allocs/op
41BenchmarkDivision/big.Int_128/64-4 20000000 98.2 ns/op 8 B/op 1 allocs/op
42BenchmarkDivision/big.Int_128/128-4 30000000 53.4 ns/op 48 B/op 1 allocs/op
43
44BenchmarkString/Uint128-4 10000000 173 ns/op 48 B/op 1 allocs/op
45BenchmarkString/big.Int-4 5000000 350 ns/op 144 B/op 3 allocs/op
46```
Note: See TracBrowser for help on using the repository browser.