Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

High-performance timer implementation based on 5-level time wheel. 高性能定时器(5级时间轮,最小堆)[从零实现]

License

NotificationsYou must be signed in to change notification settings

antlabs/timer

Repository files navigation

Gocodecov

timer是高性能定时器库

feature

  • 支持一次性定时器
  • 支持周期性定时器
  • 支持多种数据结构后端,最小堆,5级时间轮

一次性定时器

import ("github.com/antlabs/timer""log")funcmain() {tm:=timer.NewTimer()tm.AfterFunc(1*time.Second,func() {log.Printf("after\n")        })tm.AfterFunc(10*time.Second,func() {log.Printf("after\n")        })tm.Run()}

周期性定时器

import ("github.com/antlabs/timer""log")funcmain() {tm:=timer.NewTimer()tm.ScheduleFunc(1*time.Second,func() {log.Printf("schedule\n")        })tm.Run()}

自定义周期性定时器

实现时间翻倍定时的例子

typecurstomTeststruct {countint}// 只要实现Next接口就行func (c*curstomTest)Next(now time.Time) (rv time.Time) {rv=now.Add(time.Duration(c.count)*time.Millisecond*10)c.count++return}funcmain() {tm:=timer.NewTimer(timer.WithMinHeap())node:=tm.CustomFunc(&curstomTest{count:1},func() {log.Printf("%v\n",time.Now())        })tm.Run()}

取消某一个定时器

import ("log""time""github.com/antlabs/timer")funcmain() {tm:=timer.NewTimer()// 只会打印2 time.Secondtm.AfterFunc(2*time.Second,func() {log.Printf("2 time.Second")})// tk3 会被 tk3.Stop()函数调用取消掉tk3:=tm.AfterFunc(3*time.Second,func() {log.Printf("3 time.Second")})tk3.Stop()//取消tk3tm.Run()}

选择不同的的数据结构

import ("github.com/antlabs/timer""log")funcmain() {tm:=timer.NewTimer(timer.WithMinHeap())// 选择最小堆,默认时间轮}

benchmark

github.com/antlabs/timer 性能最高

goos: linuxgoarch: amd64pkg: benchmarkBenchmark_antlabs_Timer_AddTimer/N-1m-16         9177537       124 ns/opBenchmark_antlabs_Timer_AddTimer/N-5m-16        10152950       128 ns/opBenchmark_antlabs_Timer_AddTimer/N-10m-16        9955639       127 ns/opBenchmark_RussellLuo_Timingwheel_AddTimer/N-1m-16          5316916       222 ns/opBenchmark_RussellLuo_Timingwheel_AddTimer/N-5m-16          5848843       218 ns/opBenchmark_RussellLuo_Timingwheel_AddTimer/N-10m-16         5872621       231 ns/opBenchmark_ouqiang_Timewheel/N-1m-16                         720667      1622 ns/opBenchmark_ouqiang_Timewheel/N-5m-16                         807018      1573 ns/opBenchmark_ouqiang_Timewheel/N-10m-16                        666183      1557 ns/opBenchmark_Stdlib_AddTimer/N-1m-16                          8031864       144 ns/opBenchmark_Stdlib_AddTimer/N-5m-16                          8437442       151 ns/opBenchmark_Stdlib_AddTimer/N-10m-16                         8080659       167 ns/op

About

High-performance timer implementation based on 5-level time wheel. 高性能定时器(5级时间轮,最小堆)[从零实现]

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp