Go port of A Very Fast 64–Bit Date Algorithm, based on A Very Fast 64–Bit Date Algorithm: 30–40% faster by Ben Joffe. Special thanks to @benjoffe for writing such an easy to follow blog post and implementation.
go get github.com/xnacly/go-fast-datepackage main
import (
"fmt"
"time"
"github.com/xnacly/go-fast-date"
)
func main() {
date := gofastdate.FromUnix(time.Now().Unix())
fmt.Println(date)
fmt.Println(date.Time())
fmt.Println(date.Unix(), date.Time().Unix())
}go-fast-date implements Ben Joffe's very fast 64-bit date
algorithm and provides date round trip functions for
time.Time compatibility:
gofastdate.FromUnixgofastdate.FromTimegofastdate.Date.Timegofastdate.Date.Unix
While not depending on any dependency and striving to match Joffe's implementation, where applicable.
Benchmarks focus on comparing gofastdate.FromUnix with
time.Unix. Run with go test ./... -bench=.
- micro benchmark: fastdate
~13%faster - stress: fastdate
~4.2xfaster (8.98ms vs 37.38ms)
goos: linux
goarch: amd64
pkg: github.com/xnacly/go-fast-date
cpu: Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
BenchmarkTimeUnix-4 21141045 56.21 ns/op
BenchmarkGoFastDateUnix-4 24253924 48.90 ns/op
BenchmarkStressTimeUnixToDate-4 32 37382839 ns/op
BenchmarkStressGoFastDateFromUnix-4 132 8977516 ns/op
PASS
ok github.com/xnacly/go-fast-date 6.369s
- micro benchmark: fastdate
~32%faster - stress: fastdate
~4.1xfaster (5.16ms vs 21.41ms)
goos: linux
goarch: amd64
pkg: github.com/xnacly/go-fast-date
cpu: AMD Ryzen 7 3700X 8-Core Processor
BenchmarkTimeUnix-16 35375085 37.20 ns/op
BenchmarkGoFastDateUnix-16 47718576 25.21 ns/op
BenchmarkStressTimeUnixToDate-16 56 21414260 ns/op
BenchmarkStressGoFastDateFromUnix-16 232 5158183 ns/op
PASS
ok github.com/xnacly/go-fast-date 5.463s