dimanche 15 novembre 2015

How to benchmark if I need a reset in each iteration?

I've written a small Sudoku solver using backtracking. Now I want to benchmark the speed of this function. Here is my current code:

type Board struct {
    Cells [9][9]int
}

func BenchmarkBacktrack(b *testing.B) {
    for i := 0; i < b.N; i++ {
        b.StopTimer()
        // prevent the modification of the orignal board
        copy := &Board{
            Cells: exampleBoard.Cells,
        }
        b.StartTimer()
        copy.Backtrack()
     }
}

Since &Board is pointer I would solve the Sudoku in the first iteration and in the next one I would backtrack a solved board. Therefore, I reset the board at the beginning of each iteration. exampleBoard is filled with sample values.

Is their a better way to benchmark the function without stopping and restarting the timer over and over?

And wouldn't cost the function calls a small amount of time that that impacts the benchmark?

Aucun commentaire:

Enregistrer un commentaire