etime

package
v0.1.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2021 License: MIT Imports: 5 Imported by: 2

README

etime Build Status

etime extend golang's time package, to provide more and better features.

Install

go get github.com/go-eden/common/etime

Usage

This section shows all features, and their usage.

Current Timestamp

This feature works like Java's System.currentTimeMillis(), it will return int64 value directly:

  • NowSecond: obtains the current second, use syscall for better performance
  • NowMillisecond: obtains the current microsecond, use syscall for better performance
  • NowMicrosecond: obtains the current millisecond, use syscall for better performance

For better performance, Now* didn't use time.Now(), because it's a bit slow.

Demo
package main

import (
	"github.com/go-eden/common/etime"
	"time"
)

func main() {
	println(etime.NowSecond())
	println(etime.NowMillisecond())
	println(etime.NowMicrosecond())

	println(time.Now().Unix())
	println(time.Now().UnixNano() / 1e6)
	println(time.Now().UnixNano() / 1e3)
}
Performance

In my benchmark, the performance of etime.NowSecond was about 40 ns/op, the performance of time.Now() was about 68 ns/op:

BenchmarkNowSecond-12         	29296284	        39.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkNowMillisecond-12    	29361312	        40.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkNowMicrosecond-12    	29742286	        40.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkTimeNow-12           	15010953	        70.0 ns/op	       0 B/op	       0 allocs/op

Under the same hardware environment, Java's System.currentTimeMillis() was like this:

Benchmark               Mode  Cnt   Score   Error  Units
TimestampBenchmark.now  avgt    9  25.697 ± 0.139  ns/op

Some library may be sensitive to this 28ns optimization, like slf4go.

By the way, System.currentTimeMillis()'s implementation was similar with etime.NowSecond:

jlong os::javaTimeMillis() {
  timeval time;
  int status = gettimeofday(&time, NULL);
  assert(status != -1, "bsd error");
  return jlong(time.tv_sec) * 1000  +  jlong(time.tv_usec / 1000);
}

So, there should have room for improvement.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NowMicrosecond

func NowMicrosecond() int64

NowMicrosecond obtains the current microsecond, use syscall for better performance

func NowMillisecond

func NowMillisecond() int64

NowMillisecond obtains the current millisecond, use syscall for better performance

func NowMs added in v0.1.5

func NowMs() int64

NowMs is the alise for NowMillisecond

func NowNs added in v0.1.5

func NowNs() int64

NowNs returns the current nanosecond

func NowSecond

func NowSecond() uint32

NowSecond obtains the current second, use syscall for better performance

func NowUs added in v0.1.5

func NowUs() int64

NowUs is the alise for NowMicrosecond

func UptimeMs added in v0.1.5

func UptimeMs() int64

func UptimeUs added in v0.1.5

func UptimeUs() int64

Types

type Interval added in v0.1.12

type Interval struct {
	// contains filtered or unexported fields
}

func NewInterval added in v0.1.12

func NewInterval(d time.Duration, f func()) *Interval

func (*Interval) Close added in v0.1.12

func (t *Interval) Close()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL