timer

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2021 License: MIT Imports: 6 Imported by: 1

README

timer

PkgGoDev Build Status codecov Go Report Card LICENSE

Package timer provides functionality for measuring time. Much lower CPU overhead when using use_cgo tags during the idle period of system.

Feature

  • cgo/go
  • Ticker
  • TickFunc
  • Timer
  • Sleep
  • After

Get started

Install
go get github.com/hslam/timer
Import
import "github.com/hslam/timer"
Usage
Example
package main

import (
	"flag"
	"fmt"
	"github.com/hslam/timer"
)

func main() {
	t := *flag.String("t", "Sleep", "use timer")
	flag.Parse()
	fmt.Println(timer.Tag)
	switch t {
	case "Ticker":
		t := timer.NewTicker(timer.Millisecond)
		defer t.Stop()
		for range t.C {
			//todo
		}
	case "TickFunc":
		t := timer.TickFunc(timer.Millisecond, func() {
			//todo
		})
		defer t.Stop()
		select {}
	case "Timer":
		t := timer.NewTimer(timer.Millisecond)
		defer t.Stop()
		for range t.C {
			t.Reset(timer.Millisecond)
			//todo
		}
	case "Sleep":
		for {
			timer.Sleep(timer.Millisecond)
			//todo
		}
	case "After":
		for {
			select {
			case <-timer.After(timer.Millisecond):
				//todo
			}
		}
	default:
		fmt.Println("use Ticker, TickFunc, Timer, Sleep or After")
	}
}
Build
go
go build
cgo
go build -tags=use_cgo
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

timer was written by Meng Huang.

Documentation

Overview

Package timer provides functionality for measuring time.

Index

Constants

View Source
const (
	// Nanosecond represents the nano second time.Duration.
	Nanosecond time.Duration = 1
	// Microsecond represents the micro second time.Duration.
	Microsecond = 1000 * Nanosecond
	// Millisecond represents the milli second time.Duration.
	Millisecond = 1000 * Microsecond
	// Second represents the second time.Duration.
	Second = 1000 * Millisecond
	// Minute represents the minute time.Duration.
	Minute = 60 * Second
	// Hour represents the hour time.Duration.
	Hour = 60 * Minute
)

Variables

View Source
var Tag = "!use_cgo"

Tag is the sleep type.

Functions

func After

func After(d time.Duration) <-chan time.Time

After waits for the duration to elapse and then sends the current time on the returned channel. It is equivalent to NewTimer(d).C. The underlying Timer is not recovered by the garbage collector until the timer fires. If efficiency is a concern, use NewTimer instead and call Timer.Stop if the timer is no longer needed.

func Sleep

func Sleep(d time.Duration)

Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.

func Tick

func Tick(d time.Duration) <-chan time.Time

Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks". Unlike NewTicker, Tick will return nil if d <= 0.

Types

type Ticker

type Ticker struct {
	C <-chan time.Time // The channel on which the ticks are delivered.
	// contains filtered or unexported fields
}

A Ticker holds a channel that delivers `ticks' of a clock at intervals.

func NewTicker

func NewTicker(d time.Duration) *Ticker

NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.

func TickFunc

func TickFunc(d time.Duration, f func()) *Ticker

TickFunc returns a new Ticker containing a func f that will call in its own goroutine with a period specified by the duration argument. It returns a Ticker that can be used to cancel the call using its Stop method.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous "tick".

type Timer

type Timer struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

The Timer type represents a single event. When the Timer expires, the current time will be sent on C, unless the Timer was created by AfterFunc. A Timer must be created with NewTimer or AfterFunc.

func AfterFunc

func AfterFunc(d time.Duration, f func()) *Timer

AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.

func NewTimer

func NewTimer(d time.Duration) *Timer

NewTimer creates a new Timer that will send the current time on its channel after at least duration d.

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration) bool

Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped.

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.

Jump to

Keyboard shortcuts

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