download92000 2015-11-15 23:07
浏览 50
已采纳

如果每次迭代都需要重置,如何进行基准测试?

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 impacts the benchmark?

  • 写回答

2条回答 默认 最新

  • dongshungai4857 2015-11-17 13:34
    关注

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

    Of course they would. So does the for loop, which is included in the benchmark. Plus overhead of calling copy.Backtrack function. But the thing is, this should be all irrelevant, unless you're benchmarking a single operation taking nanoseconds (in which case you shouldn't). Creation of an empty board is probably a trivial operation, so I wouldn't touch the timers at all. If it's not trivial, then you're doing it right – call StopTimer. This is exactly why it was invented:

    StopTimer stops timing a test. This can be used to pause the timer while performing complex initialization that you don't want to measure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗