Revision: 3854 http://trac.macosforge.org/projects/ruby/changeset/3854 Author: ernest.prabhakar@gmail.com Date: 2010-03-24 13:38:06 -0700 (Wed, 24 Mar 2010) Log Message: ----------- Refactored Benchmark class Modified Paths: -------------- MacRuby/trunk/sample-macruby/Scripts/gcd/benchmarks.rb Modified: MacRuby/trunk/sample-macruby/Scripts/gcd/benchmarks.rb =================================================================== --- MacRuby/trunk/sample-macruby/Scripts/gcd/benchmarks.rb 2010-03-24 20:37:55 UTC (rev 3853) +++ MacRuby/trunk/sample-macruby/Scripts/gcd/benchmarks.rb 2010-03-24 20:38:06 UTC (rev 3854) @@ -1,15 +1,24 @@ #!/usr/local/bin/macruby +require 'dispatch' require 'benchmark' -require 'dispatch' +class Benchmark + def self.repeat(count, label="", &block) + raise "count: #{count} < 1" if count < 1 + block.call + t = measure {count.times &block} / count + Tms.new(*t.to_a[1..-1], label).inspect + end +end + $max_tasks = 256 -$benched = $reps = 100 +$reps = 1024 $folds = 32 $results = nil#[] puts "GCD Benchmarks" -puts "Tasks: #{$max_tasks}\tFolded: #{$folds}\tReps:\t#{$reps}\n" +puts "Tasks: #{$max_tasks}\tFolded: #{$folds}\tReps:\t#{$reps}" def work_function(i) x = 1.0+i*i @@ -29,19 +38,19 @@ Dispatch::Queue.concurrent.apply(n) {|i| work_function(i)} end -def job(n) +def concur(n) j = Dispatch::Job.new n.times {|i| j.add { work_function(i) }} j.join end -def queue(n) +def serial(n) q = Dispatch::Queue.new('org.macruby.gcd.serial') n.times {|i| q.async {work_function(i)}} q.sync { } end -def multi(n) +def multiq(n) g = Dispatch::Group.new n.times do |i| Dispatch::Queue.new("org.macruby.gcd.multi.#{i}").async(g) {work_function(i)} @@ -49,27 +58,19 @@ g.wait end - -def run(x, label, &block) - (x.nil?) ? yield : x.report(label) {$reps.times &block} +def bench(method, count=1) + proc = Proc.new { send(method.to_sym, count) } + t = Benchmark.measure("%6s" % method, &proc) + t_msec = t.real*1000 + puts "#{t.label}: %5.2f millisec (avg: %5.2f microsec)" % [t_msec, t_msec*1000/count] end -def run_all(x, n) - puts "#{n} tasks" if not x.nil? - run(x,"looped") { iter(n) } - run(x,"p_loop") { p_iter(n) } - run(x," apply") { apply(n) } - run(x,"concur") { job(n) } - run(x,"serial") { queue(n) } - run(x,"multiq") { multi(n) } - puts -end - n = 1 -width = 6 while n <= $max_tasks do - run_all(nil, n) - Benchmark.bm(width) { |x| run_all(x, n) } + puts "\n#{n} tasks" + %w(iter p_iter apply concur serial multiq).each do |s| + bench(s, n) + end n *= 2 end