初心
想要在VS Code比较方便的调试Go代码的性能,了解到基准测试对此很有帮助,但默认VS Code执行 Go 的基准测试默认的benchtime为1秒,但测试性能时需要设置为更多秒
办法
在VS Code可以通过launch.json执行与调试代码,其中可配置benchtime。
{
"version": "0.2.0",
"configurations": [
//以下是新建的方法,主要是看args中,第一个参数与第二参数连接起来,相当于命令行的: -test.bench=.
//args中可设置多个参数
{
"name": "Launch test function",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": [
"-test.bench=.",
"-test.benchtime=3s"
]
},
// 以下是默认生成的执行方法
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": [],
"showLog": true
}
]
}
Go对计算时间的操作
// 暂停计时:用于循环内的初始化,与StartTimer配合使用
b.StopTimer()
// 继续启动计时
b.StartTimer()
// 重置计时
b.ResetTimer()