本文簡要介紹ruby語言中 Benchmark.bmbm
的用法。
用法
bmbm(width = 0) { |job| ... }
有時基準測試結果會出現偏差,因為較早執行的代碼遇到的垃圾收集開銷與稍後運行的代碼不同。 bmbm
嘗試通過運行兩次測試來最小化這種影響,第一次是為了讓運行時環境穩定而進行預演,第二次是真正的。 GC.start
在每個實際計時開始之前執行;此費用不包括在時間安排中。但實際上, bmbm
能做的隻有這麽多,並且不能保證結果與垃圾收集和其他影響隔離。
因為 bmbm
需要兩次通過測試,它可以計算出所需的標簽寬度。
require 'benchmark'
array = (1..1000000).map { rand }
Benchmark.bmbm do |x|
x.report("sort!") { array.dup.sort! }
x.report("sort") { array.dup.sort }
end
生成:
Rehearsal ----------------------------------------- sort! 1.440000 0.010000 1.450000 ( 1.446833) sort 1.440000 0.000000 1.440000 ( 1.448257) -------------------------------- total: 2.890000sec user system total real sort! 1.460000 0.000000 1.460000 ( 1.458065) sort 1.450000 0.000000 1.450000 ( 1.455963)
bmbm
產生一個 Benchmark::Job 對象並返回一個 Benchmark::Tms
對象數組。
相關用法
- Ruby Benchmark.bm用法及代碼示例
- Ruby Benchmark.benchmark用法及代碼示例
- Ruby Benchmark.measure用法及代碼示例
- Ruby Benchmark模塊用法及代碼示例
- Ruby BigMath.cos用法及代碼示例
- Ruby Binding.local_variable_defined?用法及代碼示例
- Ruby BigDecimal.self >用法及代碼示例
- Ruby BigMath.sin用法及代碼示例
- Ruby BasicSocket.send用法及代碼示例
- Ruby BigDecimal.self >=用法及代碼示例
- Ruby Base64.encode64用法及代碼示例
- Ruby Bundler.setup用法及代碼示例
- Ruby BigDecimal.floor用法及代碼示例
- Ruby BigDecimal.to_s用法及代碼示例
- Ruby BigDecimal.to_d用法及代碼示例
- Ruby BigDecimal.save_rounding_mode用法及代碼示例
- Ruby BigDecimal modulo用法及代碼示例
- Ruby BigDecimal infinite?用法及代碼示例
- Ruby BigDecimal.to_digits用法及代碼示例
- Ruby BigDecimal.add用法及代碼示例
- Ruby BasicObject.equal?用法及代碼示例
- Ruby Buffer.slice用法及代碼示例
- Ruby BigDecimal.save_exception_mode用法及代碼示例
- Ruby BasicSocket.remote_address用法及代碼示例
- Ruby BigDecimal to_f()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Benchmark.bmbm。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。