<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## Banchmark 用法: ~~~ func BenchmarkExample(b *testing.B) { // 與性能測試無關的代碼 b.ResetTimer() for i := 0; i < b.N; i++ { // 測試代碼 fmt.Println("hello") } b.StopTimer() // 與性能測試無關的代碼 } ~~~ 示例: go test -bench=. go test -bench=. -benchmem ~~~ // 測試直接拼接字符串 func BenchmarkJoinString(b *testing.B) { str := []string{"php", "nodejs", "java", "go", "c"} b.ResetTimer() for i := 0; i < b.N; i++ { result := "" for _, v := range str { result += v } } b.StopTimer() } func BenchmarkBufferWriteString(b *testing.B) { str := []string{"php", "nodejs", "java", "go", "c"} b.ResetTimer() for i := 0; i < b.N; i++ { var buf bytes.Buffer for _, v := range str { buf.WriteString(v) } } b.StopTimer() } output=> 方法 運行次數 平均耗時 內存占用 內存操作次數 BenchmarkJoinString-8 7218064 157 ns/op 64 B/op 4 allocs/op BenchmarkBufferWriteString-8 17479418 61.4 ns/op 64 B/op 1 allocs/op ~~~ ### 并行測試 func (b*B) RunParallel(body func(*PB))會以并行的方式執行給定的基準測試。 RunParallel會創建出多個goroutine,并將b.N分配給這些goroutine執行, 其中goroutine數量的默認值為GOMAXPROCS。用戶如果想要增加非CPU受限(non-CPU-bound)基準測試的并行性, 那么可以在RunParallel之前調用SetParallelism 。RunParallel通常會與-cpu標志一同使用。 ~~~ func BenchmarkSplitParallel(b *testing.B) { // b.SetParallelism(1) // 設置使用的CPU數 b.RunParallel(func(pb *testing.PB) { for pb.Next() { Split("枯藤老樹昏鴉", "老") } }) } ~~~ 執行一下基準測試: ~~~ split $ go test -bench=. goos: darwin goarch: amd64 pkg: github.com/pprof/studygo/code_demo/test_demo/split BenchmarkSplit-8 10000000 131 ns/op BenchmarkSplitParallel-8 50000000 36.1 ns/op PASS ok github.com/pprof/studygo/code_demo/test_demo/split 3.308s ~~~ 還可以通過在測試命令后添加-cpu參數如go test -bench=. -cpu 1來指定使用的CPU數量 ### TestMain 通過在`*_test.go`文件中定義TestMain函數來可以在測試之前進行額外的設置(setup)或在測試之后進行拆卸(teardown)操作。 如果測試文件包含函數:`func TestMain(m *testing.M)`那么生成的測試會先調用 TestMain(m),然后再運行具體測試。TestMain運行在主goroutine中, 可以在調用 m.Run前后做任何設置(setup)和拆卸(teardown)。退出測試的時候應該使用m.Run的返回值作為參數調用os.Exit。 一個使用TestMain來設置Setup和TearDown的示例如下: ~~~ func TestMain(m *testing.M) { fmt.Println("write setup code here...") // 測試之前的做一些設置 // 如果 TestMain 使用了 flags,這里應該加上flag.Parse() retCode := m.Run() // 執行測試 fmt.Println("write teardown code here...") // 測試之后做一些拆卸工作 os.Exit(retCode) // 退出測試 } ~~~ 需要注意的是:在調用TestMain時, flag.Parse并沒有被調用。所以如果TestMain 依賴于command-line標志 (包括 testing 包的標記), 則應該顯示的調用flag.Parse
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看