java - benchmark

來源:趣味經驗館 1.15W

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

java benchmark是什麼,讓我們一起了解一下?

benchmark就是基準測試,是一種測試代碼性能的方法,同時也可以用來識別某段代碼的CPU或者內存效率問題。許多開發人員會用基準測試來測試不同的併發模式,或者用基準測試來輔助配置工作池的數量,以保證能最大化系統的吞吐量。

和單元測試的文件名一樣,基準測試的文件名也必須以“_test.go”結尾。另外,基準測試函數必須以Benchmark開頭,接受一個指向testing.B類型的指針作為唯一參數。

java benchmark

實戰操作:如何比較Golang標準庫裏3種將整數轉為字符串的性能?

package benchmark_test import ("fmt""strconv""testing") func BenchmarkSprintf(b *testing.B) {b.ResetTimer()number := int64(10)for i := 0; i < b.N; i++ {fmt.Sprintf("%d", number)}} func BenchmarkItoa(b *testing.B) {b.ResetTimer()number := 10for i := 0; i < b.N; i++ {strconv.Itoa(number)}} func BenchmarkFormat(b *testing.B) {b.ResetTimer()number := int64(10)for i := 0; i < b.N; i++ {strconv.FormatInt(number, 10)}}

熱門標籤