github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/etime/README.md (about)

     1  # etime [![Build Status](https://travis-ci.org/go-eden/etime.svg?branch=master)](https://travis-ci.org/go-eden/etime)
     2  
     3  `etime` extend golang's `time` package, to provide more and better features.
     4  
     5  # Install
     6  
     7  ```bash
     8  go get github.com/go-eden/common/etime
     9  ```
    10  
    11  # Usage
    12   
    13  This section shows all features, and their usage.
    14   
    15  ## Current Timestamp
    16  
    17  This feature works like Java's `System.currentTimeMillis()`, it will return `int64` value directly:
    18  
    19  + `NowSecond`: obtains the current second, use syscall for better performance
    20  + `NowMillisecond`: obtains the current microsecond, use syscall for better performance
    21  + `NowMicrosecond`: obtains the current millisecond, use syscall for better performance
    22  
    23  For better performance, `Now*` didn't use `time.Now()`, because it's a bit slow. 
    24  
    25  ### Demo
    26  
    27  ```go
    28  package main
    29  
    30  import (
    31  	"github.com/go-eden/common/etime"
    32  	"time"
    33  )
    34  
    35  func main() {
    36  	println(etime.NowSecond())
    37  	println(etime.NowMillisecond())
    38  	println(etime.NowMicrosecond())
    39  
    40  	println(time.Now().Unix())
    41  	println(time.Now().UnixNano() / 1e6)
    42  	println(time.Now().UnixNano() / 1e3)
    43  }
    44  ```
    45  
    46  ### Performance
    47  
    48  In my benchmark, the performance of `etime.NowSecond` was about `40 ns/op`, the performance of `time.Now()` was about `68 ns/op`:
    49  
    50  ```
    51  BenchmarkNowSecond-12         	29296284	        39.8 ns/op	       0 B/op	       0 allocs/op
    52  BenchmarkNowMillisecond-12    	29361312	        40.7 ns/op	       0 B/op	       0 allocs/op
    53  BenchmarkNowMicrosecond-12    	29742286	        40.1 ns/op	       0 B/op	       0 allocs/op
    54  BenchmarkTimeNow-12           	15010953	        70.0 ns/op	       0 B/op	       0 allocs/op
    55  ```
    56  
    57  Under the same hardware environment, Java's `System.currentTimeMillis()` was like this:
    58  
    59  ```
    60  Benchmark               Mode  Cnt   Score   Error  Units
    61  TimestampBenchmark.now  avgt    9  25.697 ± 0.139  ns/op
    62  ```
    63  
    64  Some library may be sensitive to this `28ns` optimization, like [slf4go](https://github.com/go-eden/slf4go). 
    65  
    66  By the way, `System.currentTimeMillis()`'s implementation was similar with `etime.NowSecond`:
    67  
    68  ```c++
    69  jlong os::javaTimeMillis() {
    70    timeval time;
    71    int status = gettimeofday(&time, NULL);
    72    assert(status != -1, "bsd error");
    73    return jlong(time.tv_sec) * 1000  +  jlong(time.tv_usec / 1000);
    74  }
    75  ```
    76  
    77  So, there should have room for improvement.
    78  
    79  # License
    80  
    81  MIT