How to resolve the algorithm Time a function step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Time a function step by step in the Go programming language

Table of Contents

Problem Statement

Write a program which uses a timer (with the least granularity available on your system) to time how long a function takes to execute. Whenever possible, use methods which measure only the processing time used by the current process; instead of the difference in system time between start and finish, which could include time used by other processes on the computer. This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Time a function step by step in the Go programming language

Function: Empty()

  • This function is empty, it has no implementation. Its sole purpose is to be called without doing anything.

Function: Count()

  • This function counts from 0 to a million (1e6). It serves as a benchmark for comparison.

Benchmark: Empty()

  • It creates a benchmark named Empty that calls the Empty() function multiple times. The number of iterations is determined by b.N.

Benchmark: Count()

  • Similar to the previous benchmark, it creates another benchmark named Count that calls the Count() function multiple times.

Example Usage:

  • Package main defines two functions, empty and count, which are similar to the functions in the previous packages.
  • The main function uses the built-in testing.Benchmark function to benchmark both empty and count.
  • It prints the results to the console, showing the time it took to run each benchmark.

Fine-Grained Benchmarking:

  • Package main demonstrates a more fine-grained approach to benchmarking using the time package to measure the exact time taken by each function.
  • The from() function is used to calculate and print the time elapsed since a specific reference time point.
  • The empty and count functions are modified to measure the time they take to execute.

Source code in the go programming language

package empty

func Empty() {}

func Count() {
    // count to a million
    for i := 0; i < 1e6; i++ {
    }
}


package empty

import "testing"

func BenchmarkEmpty(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Empty()
    }
}

func BenchmarkCount(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Count()
    }
}


package main

import (
    "fmt"
    "testing"
)

func empty() {}

func count() {
    for i := 0; i < 1e6; i++ {
    }
}

func main() {
    e := testing.Benchmark(func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            empty()
        }
    })
    c := testing.Benchmark(func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            count()
        }
    })
    fmt.Println("Empty function:    ", e)
    fmt.Println("Count to a million:", c)
    fmt.Println()
    fmt.Printf("Empty: %12.4f\n", float64(e.T.Nanoseconds())/float64(e.N))
    fmt.Printf("Count: %12.4f\n", float64(c.T.Nanoseconds())/float64(c.N))
}


package main

import (
    "fmt"
    "time"
)

func from(t0 time.Time) {
    fmt.Println(time.Now().Sub(t0))
}

func empty() {
    defer from(time.Now())
}

func count() {
    defer from(time.Now())
    for i := 0; i < 1e6; i++ {
    }
}

func main() {
    empty()
    count()
}


  

You may also check:How to resolve the algorithm Reverse a string step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the jq programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Clojure programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the Ada programming language