MCPcopy
hub / github.com/pterm/pterm / PrintAverageExecutionTime

Function PrintAverageExecutionTime

putils/stats.go:11–30  ·  view source on GitHub ↗

PrintAverageExecutionTime times the average execution time of a function.

(count int, f func(i int) error)

Source from the content-addressed store, hash-verified

9
10// PrintAverageExecutionTime times the average execution time of a function.
11func PrintAverageExecutionTime(count int, f func(i int) error) error {
12 var total time.Duration
13 for i := 0; i < count; i++ {
14 start := time.Now()
15 err := f(i)
16 duration := time.Since(start)
17
18 if err != nil {
19 return fmt.Errorf("error while calculating average execution time: %w", err)
20 }
21
22 total += duration
23 }
24
25 averageExecutionTime := total / time.Duration(count)
26
27 pterm.Printfln(pterm.Cyan("Average execution time: %s"), pterm.NewStyle(pterm.Bold, pterm.FgLightCyan).Sprint(averageExecutionTime))
28
29 return nil
30}

Callers

nothing calls this directly

Calls 3

PrintflnFunction · 0.92
NewStyleFunction · 0.92
SprintMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…