Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use time value of benchmark

I have written a benchmark for my chess engine in Go:

func BenchmarkStartpos(b *testing.B) {
    board := ParseFen(startpos)
    for i := 0; i < b.N; i++ {
        Perft(&board, 5)
    }
}

I see this output when it runs:

goos: darwin
goarch: amd64
BenchmarkStartpos-4           10     108737398 ns/op
PASS
ok      _/Users/dylhunn/Documents/go-chess  1.215s

I want to use the time per execution (in this case, 108737398 ns/op) to compute another value, and also print it as a result of the benchmark. Specifically, I want to output nodes per second, which is given as the result of the Perft call divided by the time per call.

How can I access the time the benchmark took to execute, so I can print my own derived results?

like image 453
dylhunn Avatar asked May 09 '17 07:05

dylhunn


People also ask

What is a benchmark time?

Benchmark Time means 11:59 p.m. (local time wherever applied) on the Benchmark Date. “Bill of Sale” means the bill of sale to be entered into by and between the Company andPurchaser or one of its Affiliates, in form and substance mutually agreed by the Parties.

How is benchmark calculated?

Next, the benchmark scores are computed by averaging the scores of the related survey items. Finally, the scores are standardized around the mean of the 3-year cohort so that respondents' scores have a mean of 50, weighted by full- and part-time attendance status, and a standard deviation of 25.


1 Answers

You may use the testing.Benchmark() function to manually measure / benchmark "benchmark" functions (that have the signature of func(*testing.B)), and you get the result as a value of testing.BenchmarkResult, which is a struct with all the details you need:

type BenchmarkResult struct {
    N         int           // The number of iterations.
    T         time.Duration // The total time taken.
    Bytes     int64         // Bytes processed in one iteration.
    MemAllocs uint64        // The total number of memory allocations.
    MemBytes  uint64        // The total number of bytes allocated.
}

The time per execution is returned by the BenchmarkResult.NsPerOp() method, you can do whatever you want to with that.

See this simple example:

func main() {
    res := testing.Benchmark(BenchmarkSleep)
    fmt.Println(res)
    fmt.Println("Ns per op:", res.NsPerOp())
    fmt.Println("Time per op:", time.Duration(res.NsPerOp()))
}

func BenchmarkSleep(b *testing.B) {
    for i := 0; i < b.N; i++ {
        time.Sleep(time.Millisecond * 12)
    }
}

Output is (try it on the Go Playground):

     100      12000000 ns/op
Ns per op: 12000000
Time per op: 12ms
like image 83
icza Avatar answered Oct 24 '22 15:10

icza