mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 16:12:42 +08:00
watch
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package stopwatch
|
package stopwatch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "unsafe"
|
_ "unsafe"
|
||||||
@ -10,6 +11,7 @@ type watch struct {
|
|||||||
startTime int64
|
startTime int64
|
||||||
elapsed int64
|
elapsed int64
|
||||||
isRunning bool
|
isRunning bool
|
||||||
|
count int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartNew() *watch {
|
func StartNew() *watch {
|
||||||
@ -21,7 +23,7 @@ func StartNew() *watch {
|
|||||||
|
|
||||||
func (w *watch) Start() {
|
func (w *watch) Start() {
|
||||||
if !w.isRunning {
|
if !w.isRunning {
|
||||||
w.startTime = runtimeNano()
|
w.startTime = w.RuntimeNano()
|
||||||
w.isRunning = true
|
w.isRunning = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,22 +31,20 @@ func (w *watch) Start() {
|
|||||||
// 将运行时间重置为零,并开始测量运行时间。
|
// 将运行时间重置为零,并开始测量运行时间。
|
||||||
func (w *watch) Restart() {
|
func (w *watch) Restart() {
|
||||||
w.elapsed = 0
|
w.elapsed = 0
|
||||||
w.startTime = runtimeNano()
|
w.startTime = w.RuntimeNano()
|
||||||
w.isRunning = true
|
w.isRunning = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 停止时间间隔测量并将经过的时间重置为零。
|
// 停止时间间隔测量并将经过的时间重置为零。
|
||||||
func (w *watch) Reset() {
|
func (w *watch) Reset() {
|
||||||
w.elapsed = 0
|
w.elapsed = 0
|
||||||
w.startTime = runtimeNano()
|
w.startTime = w.RuntimeNano()
|
||||||
w.isRunning = false
|
w.isRunning = false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *watch) Stop() {
|
func (w *watch) Stop() {
|
||||||
if w.isRunning {
|
if w.isRunning {
|
||||||
n := runtimeNano()
|
w.elapsed += w.RuntimeNano() - w.startTime
|
||||||
num := n - w.startTime
|
|
||||||
w.elapsed += num
|
|
||||||
w.isRunning = false
|
w.isRunning = false
|
||||||
|
|
||||||
if w.elapsed < 0 {
|
if w.elapsed < 0 {
|
||||||
@ -70,14 +70,18 @@ func (w *watch) ElapsedMicroseconds() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *watch) ElapsedNanoseconds() int64 {
|
func (w *watch) ElapsedNanoseconds() int64 {
|
||||||
now := runtimeNano()
|
|
||||||
num := w.elapsed
|
num := w.elapsed
|
||||||
if w.isRunning {
|
if w.isRunning {
|
||||||
num += now - w.startTime
|
num += w.RuntimeNano() - w.startTime
|
||||||
}
|
}
|
||||||
|
|
||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *watch) RuntimeNano() int64 {
|
||||||
|
atomic.AddInt32(&w.count, 1)
|
||||||
|
return runtimeNano()
|
||||||
|
}
|
||||||
|
|
||||||
//go:linkname runtimeNano runtime.nanotime
|
//go:linkname runtimeNano runtime.nanotime
|
||||||
func runtimeNano() int64
|
func runtimeNano() int64
|
||||||
|
@ -42,3 +42,13 @@ func TestStop(t *testing.T) {
|
|||||||
|
|
||||||
t.Log(watch.Elapsed())
|
t.Log(watch.Elapsed())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkNanotime(b *testing.B) {
|
||||||
|
b.RunParallel(func(p *testing.PB) {
|
||||||
|
watch := stopwatch.StartNew()
|
||||||
|
|
||||||
|
for p.Next() {
|
||||||
|
watch.ElapsedNanoseconds()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user