Permalink
Browse files

added an example to show how pool forking is faster than serial forki…

…ng in some cases.
  • Loading branch information...
1 parent 0a676e6 commit 2accafb7346a4b38fd58712a3161e056107e7bf9 @dakrone committed Jul 12, 2009
Showing with 28 additions and 2 deletions.
  1. +26 −0 examples/d.rb
  2. +2 −2 test/test_forkify.rb
View
@@ -0,0 +1,26 @@
+#!/usr/bin/env ruby
+# vim: set ts=2 sw=2 filetype=Ruby
+#
+# This example shows how pool forking can be faster than serial forking in
+# some cases.
+
+require 'forkify'
+
+#FORKIFY_DEBUG = true
+
+puts "Forkifying with a pool..."
+pool_start_time = Time.now
+result = [1, 1, 1, 1, 5, 1, 3, 2].forkify(:procs => 5, :method => :pool) { |n| puts "#{$$} sleeping for #{n}"; sleep(n); n }
+pool_stop_time = Time.now
+
+puts "Forkifying serially..."
+serial_start_time = Time.now
+result = [1, 1, 1, 1, 5, 1, 3, 2].forkify(:procs => 5, :method => :serial) { |n| puts "#{$$} sleeping for #{n}"; sleep(n); n }
+serial_stop_time = Time.now
+
+pool_time = pool_stop_time - pool_start_time
+serial_time = serial_stop_time - serial_start_time
+
+puts "Time with pool forking #{pool_time} seconds."
+puts "Time with serial forking #{serial_time} seconds."
+
View
@@ -46,12 +46,12 @@
t.check :hash_length, :expect => 3, :actual => r.size
end
- test 'array of nils' do |t|
+ test 'serial array of nils' do |t|
r = [nil, nil].forkify { |n| n }
t.check :nil_array, :expect => [nil, nil], :actual => r
end
- test 'array of nils' do |t|
+ test 'pool array of nils' do |t|
r = [nil, nil].forkify(:method => :pool) { |n| n }
t.check :nil_array, :expect => [nil, nil], :actual => r
end

0 comments on commit 2accafb

Please sign in to comment.