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

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

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条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计