benchmark/benchmark.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package benchmark import ( "fmt" "time" ) func BenchmarkFunction(fn func(), count int, rounds int) { var totalDuration time.Duration for r := 0; r < rounds; r++ { start := time.Now() for i := 0; i < count; i++ { fn() } duration := time.Since(start) totalDuration += duration } averageDuration := totalDuration / time.Duration(rounds) fmt.Printf("\nAverage time over %d rounds: %v\n", rounds, averageDuration) fmt.Printf("Average time per operation: %v\n", averageDuration/time.Duration(count)) } |