# debug.Timer
> 返回一个具名计时器，用于测量执行耗时并输出到控制台。
- 官方英文原文：https://gohugo.io/functions/debug/timer/
- 本页规范地址：https://hugozh.cn/functions/debug/timer/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：debug.Timer NAME
- 返回类型：debug.Timer
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
用 `debug.Timer` 函数测量一段代码的执行时间，这有助于发现模板中的性能瓶颈。

计时器在你实例化它时启动，在调用它的 `Stop` 方法时停止。

```go-html-template
{{ $t := debug.Timer "TestSqrt" }}
{{ range 2000 }}
  {{ $f := math.Sqrt . }}
{{ end }}
{{ $t.Stop }}
```

构建站点时使用 `--logLevel info` 命令行参数。

```sh
hugo build --logLevel info
```

结果会在构建结束时显示到控制台。计时器数量不限；如果没有停止它们，它们会在构建结束时被停止。

```text
INFO  timer:  name TestSqrt count 1002 duration 2.496017496s average 2.491035ms median 2.282291ms
```

