timing

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2025 License: MIT Imports: 4 Imported by: 0

README

timing

A simple Go package for timing your code. Its purpose is to offer a straightforward and lightweight library that enables you to benchmark particular sections of your code whenever it's necessary.

Example

package main

import (
    "fmt"
    "time"

    "github.com/seealsocn/timing"
)

func main() {
    timers := timing.NewTimers()

    timers.Start("total", "task13")

    timers.Start("task1")
    time.Sleep(time.Millisecond)
    task1Elapsed := timers.Measure("task1")

    timers.Pause("task13")

    timers.Start("task2")
    time.Sleep(time.Millisecond)
    task2Elapsed := timers.Measure("task2")

    timers.Resume("task13")

    timers.Start("task3")
    time.Sleep(time.Millisecond)
    task3Elapsed := timers.Measure("task3")

    task13Elapsed := timers.Measure("task13")
    totalElapsed := timers.Measure("total")

    fmt.Printf("Task elapsed\ntask1: %v\ntask2: %v\ntask3: %v\ntask13: %v\ntotal: %v",
        task1Elapsed, task2Elapsed, task3Elapsed, task13Elapsed, totalElapsed)

    fmt.Printf("Measure all: %+v", timers.MeasureAll())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Elapsed added in v1.1.0

func Elapsed(name string) time.Duration

Elapsed returns the elapsed time, and does not pause the timer.

func ElapsedAll added in v1.1.0

func ElapsedAll() map[string]time.Duration

ElapsedAll returns the elapsed time for all timers, and does not pause them.

func Measure

func Measure(name string) time.Duration

Measure measures the elapsed time, and pauses the timer.

func MeasureAll

func MeasureAll() map[string]time.Duration

MeasureAll measures all the timers.

func Pause

func Pause(names ...string)

Pause pauses the timer.

func PauseAll added in v1.0.1

func PauseAll()

PauseAll pauses all the timers.

func Resume

func Resume(names ...string)

Resume resumes the timer.

func Start

func Start(names ...string)

Start starts the timer.

Types

type StopWatch

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

StopWatch measures the elapsed time.

func NewStopWatch

func NewStopWatch(autoStart bool) *StopWatch

NewStopWatch creates a StopWatch.

func (*StopWatch) Elapsed

func (s *StopWatch) Elapsed() time.Duration

Elapsed returns the Duration since the last start.

func (*StopWatch) Pause

func (s *StopWatch) Pause()

Pause adds the current elapsed time to `elapsed` and resets the `start` time.

func (*StopWatch) PauseAt added in v1.0.1

func (s *StopWatch) PauseAt(at time.Time)

PauseAt adds the specified elapsed time to `elapsed` and resets the `start` time. Useful for testing.

func (*StopWatch) Restart

func (s *StopWatch) Restart()

Restart restarts the stopwatch. It sets `start` time to the current time, and resets the `end` time and `elapsed` time.

func (*StopWatch) RestartAt added in v1.0.1

func (s *StopWatch) RestartAt(at time.Time)

RestartAt restarts the stopwatch. It sets `start` time to the specified time, and resets the `end` time and `elapsed` time. Useful for testing.

func (*StopWatch) Start

func (s *StopWatch) Start()

Start starts the stopwatch. It sets the `start` time to the current time.

func (*StopWatch) StartAt added in v1.0.1

func (s *StopWatch) StartAt(at time.Time)

StartAt starts the stopwatch. It sets the `start` time to the specified time. Useful for testing.

func (*StopWatch) Stop

func (s *StopWatch) Stop()

Stop stops the timer. It sets the `end` time to the current time.

func (*StopWatch) StopAt added in v1.0.1

func (s *StopWatch) StopAt(at time.Time)

StopAt stops the timer. It sets the `end` time to the specified time. Useful for testing.

type Timers

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

Timers measures the elapsed time.

func GetTimers

func GetTimers() *Timers

GetTimers returns the default Timers.

func NewTimers

func NewTimers(label string) *Timers

NewTimers creates a new Timers.

func (*Timers) Elapsed added in v1.1.0

func (t *Timers) Elapsed(name string) time.Duration

Elapsed returns the elapsed time, and does not pause the timer.

func (*Timers) ElapsedAll added in v1.1.0

func (t *Timers) ElapsedAll() map[string]time.Duration

ElapsedAll returns the elapsed time for all timers, and does not pause them.

func (*Timers) Measure

func (t *Timers) Measure(name string) time.Duration

Measure measures the elapsed time, and pauses the timer.

func (*Timers) MeasureAll

func (t *Timers) MeasureAll() map[string]time.Duration

MeasureAll measures the elapsed time for all timers, and pauses them.

func (*Timers) Message

func (t *Timers) Message(name string) string

Message returns the formatted elapsed time in milliseconds.

func (*Timers) Pause

func (t *Timers) Pause(names ...string)

Pause pauses the timer.

func (*Timers) PauseAll added in v1.0.1

func (t *Timers) PauseAll()

PauseAll pauses all the timers.

func (*Timers) Resume

func (t *Timers) Resume(names ...string)

Resume resumes the timer.

func (*Timers) Start

func (t *Timers) Start(names ...string)

Start starts the timer.

Jump to

Keyboard shortcuts

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