Ruby/Development/Benchmark
Материал из Wiki.crossplatform.ru
(Различия между версиями)
Версия 17:10, 26 мая 2010
Содержание |
Because measure accepts code blocks, you can make it as elaborate
require "benchmark" iterations = 1000000 b = Benchmark.measure do for i in 1..iterations do x = i end end c = Benchmark.measure do iterations.times do |i| x = i end end puts b puts c
Benchmark includes a way to make completing multiple tests more convenient.
require "benchmark" iterations = 1000000 Benchmark.bm do |bm| bm.report("for:") do for i in 1..iterations do x = i end end bm.report("times:") do iterations.times do |i| x = i end end end
Benchmark inline c
require "rubygems" require "inline" require "benchmark" class CFactorial class << self inline do |builder| builder.c %q{ long factorial(int value) { long result = 1, i = 1; for (i = 1; i <= value; i++) { result *= i; } return result; } } end end end class Fixnum def factorial (1..self).inject { |a, b| a * b } end end Benchmark.bm do |bm| bm.report("ruby:") do 100000.times { 8.factorial } end bm.report("c:") do 100000.times { CFactorial.factorial(8) } end end
Benchmark reflection
require "benchmark" include Benchmark test = "this is a test" m = test.method(:length) n = 100000 bm(12) {|x| x.report("call") { n.times { m.call } } x.report("send") { n.times { test.send(:length) } } x.report("eval") { n.times { eval "test.length" } } }
Simple Benchmarking
require "benchmark" puts Benchmark.measure { 10000.times { print "." } }