GO語言"testing"包中"B.RunParallel"類型的用法及代碼示例。
用法:
func(b *B) RunParallel(body func(*PB))
RunParallel 並行運行基準測試。它創建多個 goroutine 並在它們之間分配 b.N 次迭代。 goroutine 的數量默認為 GOMAXPROCS。要增加非 CPU 綁定基準測試的並行性,請在 RunParallel RunParallel 通常與 go test -cpu 標誌一起使用之前調用 SetParallelism。
body 函數將在每個 goroutine 中運行。它應該設置任何goroutine-local 狀態,然後迭代直到 pb.Next 返回 false。它不應該使用 StartTimer StopTimer 或 ResetTimer 函數,因為它們具有全局效果。它也不應該調用 Run。
例子:
package main
import (
"bytes"
"testing"
"text/template"
)
func main() {
// Parallel benchmark for text/template.Template.Execute on a single object.
testing.Benchmark(func(b *testing.B) {
templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
// RunParallel will create GOMAXPROCS goroutines
// and distribute work among them.
b.RunParallel(func(pb *testing.PB) {
// Each goroutine has its own bytes.Buffer.
var buf bytes.Buffer
for pb.Next() {
// The loop body is executed b.N times total across all goroutines.
buf.Reset()
templ.Execute(&buf, "World")
}
})
})
}
相關用法
- GO B.ReportMetric用法及代碼示例
- GO Buffer.Bytes用法及代碼示例
- GO Buffer.Read用法及代碼示例
- GO Buffer.Len用法及代碼示例
- GO Buffer用法及代碼示例
- GO Buffer.Next用法及代碼示例
- GO Buffer.ReadByte用法及代碼示例
- GO Base用法及代碼示例
- GO Buffer.Cap用法及代碼示例
- GO Builder用法及代碼示例
- GO ByteOrder用法及代碼示例
- GO Buffer.Grow用法及代碼示例
- GO BinaryOp用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO LeadingZeros32用法及代碼示例
- GO NewFromFiles用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Time.Sub用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 B.RunParallel。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。