」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 使用 Golang 進行基準測試

使用 Golang 進行基準測試

發佈於2024-11-04
瀏覽:125

地鼠你好?

在這篇文章中,我將向您展示如何使用 #golang 測試包中內建的出色工具。您將如何測試一段程式碼或函數的效能?使用基準測試。

我們走吧。

對於此測試,我將使用經典的斐波那契數列或斐波那契數列,其由以下因素決定:

if (x 



這個序列很重要,因為它也出現在數學和自然的幾個部分中,如下所示:

Benchmark with Golang

有多種方法可以實現此程式碼,我將選擇兩種方法進行基準測試:計算它的遞歸方法和迭代方法。這些函數的主要目標是提供 位置 並傳回該位置的斐波那契數。

遞迴法

// main.go

func fibRecursive(n int) int {
    if n 



迭代法

// main.go

func fibIterative(position uint) uint {
    slc := make([]uint, position)
    slc[0] = 1
    slc[1] = 1

    if position 



這些方法並未經過最佳化,但即使對於少數情況,測試結果也有顯著差異。您將在測試中看到這一點。若要跟隨程式碼進行操作,您可以按一下此處。

現在,對於基準測試,讓我們在 _main_test.go 檔案中編寫一些測試。使用Golang關於benchmark的文檔,您可以建立要測試的函數,如下所示:

// main_test.go

// The key is to start every function you want to benchmark with the keyword Benchmark and use b *testing.B instead of t *testing.T as input 
func BenchmarkFibIterative(b *testing.B) {
    // Use this for-loop to ensure the code will behave correctly. 
    // Now, you can put the function or piece of code you want to test
    for i := 0; i 



繼續之前問一個問題:哪個比較快?

讓我們對一個較小的數字 (10) 和一個稍大的數字 (80) 進行測試。要執行基準測試,您只需執行命令:

go test -bench=函數名稱

如果您想了解更多有關此命令的信息,請查看此處。

第一次測試:position=10

//(fibIterative)
Results:
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8         24491042                42.50 ns/op
PASS
ok      playground      1.651s

我們用這張圖來分析一下:

Benchmark with Golang

根據圖像,我們有8個核心用於測試,沒有時間限制(它將運行到完成)。花了1.651s完成任務。

==== Extra ====
We got 24,491,042 iterations (computations), and each iteration (op) took 42.50 ns.

Doing some math, we can calculate how much time one op took:

42.50 ns/op with 1 ns = 1/1,000,000,000 s
op ≈ 2.35270590588e-12 s
==== Extra ====

這是一個很好的結果。讓我們檢查一下位置 10 的遞歸函數:

// Results
BenchmarkFibRecursive-8          6035011               187.8 ns/op
PASS
ok      playground      1.882s

我們可以看到,需要1.882s完成任務。

迭代函數以幾十分之一的優勢獲勝。讓我們再試試一項測試:

位置 50

// Results for the Iterative Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8         27896118                45.37 ns/op
PASS
ok      playground      2.876s

// Results for the Recursive Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibRecursive-8          6365198               186.3 ns/op
PASS
ok      playground      1.918s

哇!現在遞歸函數更快了?

讓我們以稍微大一點的數字來結束。

位置80

// Results for the Iterative Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8          5102344               229.5 ns/op
PASS
ok      playground      1.933s

// Results for the Recursive Function
// My poor PC couldn’t handle it, so I had to reduce the position to 50 just to get some results printed.

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibRecursive-8                1        44319299474 ns/op
PASS
ok      playground      44.951s

差異很大。對於位置 80,迭代方法大約需要 2 秒。對於位置 50,遞歸函數花費了大約 45 秒。這證明了當您的 Golang 專案開始變慢時對程式碼進行基準測試的重要性。

結論

如果您的生產代碼運行緩慢或不可預測地變慢,您可以使用此技術,結合pprof 或內建測試包中的其他工具,來識別和測試程式碼的執行位置不好以及如何優化它。

附註:並非所有美觀的程式碼都具有更高的效能。

額外運動

你能找到更好的方法來改進遞歸函數嗎? (提示:使用動態規劃)。本文解釋了為什麼對於一些小數字,遞歸策略會更好。

版本聲明 本文轉載於:https://dev.to/pedrobertao/benchmark-with-golang-md4?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3