Go to file
Jeffrey Paul a6243a4702 update module path, fix linting 2024-05-22 14:20:31 -07:00
.gitignore first 2024-05-18 18:37:05 -07:00
LICENSE first 2024-05-18 18:37:05 -07:00
Makefile latest 2024-05-18 18:49:03 -07:00
README.md first 2024-05-18 18:37:05 -07:00
bench.go update module path, fix linting 2024-05-22 14:20:31 -07:00
bench_test.go update module path, fix linting 2024-05-22 14:20:31 -07:00
go.mod update module path, fix linting 2024-05-22 14:20:31 -07:00
go.sum update module path, fix linting 2024-05-22 14:20:31 -07:00



TimingBench is a Go module for benchmarking the execution time of a function. It runs the function a specified number of times and returns the minimum, maximum, mean, and median execution times, along with other relevant statistics. It also supports context for cancellation.


  • Measure min, max, mean, and median execution times of a function.
  • Support for context-based cancellation.
  • Provides detailed timing statistics.


To install the timingbench package, use the following command:

go get git.eeqj.de/sneak/timingbench


Here's a simple example of how to use the timingbench module:

package main

import (

// Example function to benchmark
func sampleFunction() error {
    time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
    return nil

func main() {
    // Seed the random number generator.

    // Define the number of iterations.
    iterations := 10

    // Create a context with a timeout for cancellation.
    ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
    defer cancel()

    // Measure the execution times of the sample function.
    result, err := timingbench.TimeFunction(ctx, sampleFunction, iterations)
    if err != nil {
        fmt.Printf("Error measuring function: %v\n", err)

    // Print the timing results.


The TimingResult struct holds the timing statistics for the function executions:

type TimingResult struct {
    Min         time.Duration // Minimum execution time
    Max         time.Duration // Maximum execution time
    Mean        time.Duration // Mean execution time
    Median      time.Duration // Median execution time
    StartTime   time.Time // Start time of the benchmarking
    EndTime     time.Time // End time of the benchmarking
    Duration    time.Duration // Total duration of the benchmarking
    Iterations  int // Number of iterations


This project is licensed under the WTFPL License. See the LICENSE file for details.


Contributions are welcome! Please send patches via email.


timingbench was written by sneak (website).