Revision: 3858 http://trac.macosforge.org/projects/ruby/changeset/3858 Author: ernest.prabhakar@gmail.com Date: 2010-03-24 13:38:13 -0700 (Wed, 24 Mar 2010) Log Message: ----------- Finish sample gcd/benchmarks.rb vis-a-vis Dispatch_Compared 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:38:11 UTC (rev 3857) +++ MacRuby/trunk/sample-macruby/Scripts/gcd/benchmarks.rb 2010-03-24 20:38:13 UTC (rev 3858) @@ -1,13 +1,20 @@ #!/usr/local/bin/macruby +# +# Iterate using different GCD techniques to illustrate relative performance/overhead +# +# Inspired by: https://developer.apple.com/mac/library/samplecode/Dispatch_Compared/index.h... require 'dispatch' require 'benchmark' $max_tasks = 256 $reps = 1024 -$folds = 32 +$folds = 8 $results = nil#[] +$group = Dispatch::Group.new +$queue = Dispatch::Queue.new('org.macruby.gcd.serial') + class Benchmark def self.repeat(count, label="", &block) raise "count: #{count} < 1" if count < 1 @@ -23,11 +30,11 @@ $results[i] = x if not $results.nil? end -def iter(n) +def times(n) n.times {|i| work_function(i)} end -def p_iter(n) +def ptimes(n) n.p_times {|i| work_function(i)} end @@ -36,26 +43,23 @@ end def concur(n) - g = Dispatch::Group.new q = Dispatch::Queue.concurrent n.times do |i| - q.async(g) {work_function(i)} + q.async($group) {work_function(i)} end - g.wait + $group.wait end def serial(n) - q = Dispatch::Queue.new('org.macruby.gcd.serial') - n.times {|i| q.async {work_function(i)}} - q.sync { } + n.times {|i| $queue.async {work_function(i)}} + $queue.sync { } end def nqueue(n) - g = Dispatch::Group.new n.times do |i| - Dispatch::Queue.new("org.macruby.gcd.multi.#{i}").async(g) {work_function(i)} + Dispatch::Queue.new("org.macruby.gcd.multi.#{i}").async($group) {work_function(i)} end - g.wait + $group.wait end def njobs(n) @@ -69,12 +73,12 @@ Benchmark.repeat($reps, "%6s" % method, &proc).real*1e6/count end -METHODS = %w(iter p_iter apply concur serial nqueue njobs) +METHODS = %w(times ptimes apply concur serial nqueue njobs) TASKS = [t = 1] TASKS << t *= 2 while t < $max_tasks print "GCD BENCHMARKS\tMaxTask\t#{$max_tasks}\tFolds\t#{$folds}\tReps\t#{$reps}\n" -print "T µsec\t#{TASKS.join("\t ")}" +print "T µsec\t #{TASKS.join("\t ")}" METHODS.each do |method| print "\n#{method}" @@ -82,5 +86,5 @@ print "\t%6.2f" % bench(method, n) end end - +puts print "Results: #{$results.join("\t")}" if not $results.nil?
participants (1)
-
source_changes@macosforge.org