Created
August 17, 2009 08:27
-
-
Save alainravet/168996 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'parallel' #http://github.com/grosser/parallel | |
$stdout.sync = true # Auto-flush for interactive I/O | |
def slow_action | |
def fibo(n) | |
0 == n ? 0 : | |
1 == n ? 1 : | |
fibo(n-1) + fibo(n-2) | |
end | |
fibo(26) | |
end | |
puts "-------------------------------------------------------" | |
puts "TOTAL TIME SPENT = #{start = Time.now ; slow_action ; Time.now - start}, for 1 process in solo" | |
puts "-------------------------------------------------------\n\n" | |
(1..5).each do |nof_processes| | |
start_all = Time.now | |
results = Parallel.in_processes(nof_processes) do |i| | |
start=Time.now | |
slow_action | |
#sleep 2 | |
puts "task [#{i}] took #{Time.now-start}" | |
end | |
puts "-------------------------------------------------------" | |
puts "TOTAL TIME SPENT = #{Time.now - start_all}, for #{nof_processes} processes in parallel" | |
puts "-------------------------------------------------------\n\n" | |
end | |
# | |
(1..5).each do |nof_threads| | |
start_all = Time.now | |
results = Parallel.in_threads(nof_threads) do |i| | |
start=Time.now | |
slow_action | |
#sleep 2 | |
puts "task [#{i}] took #{Time.now-start}" | |
end | |
puts "-------------------------------------------------------" | |
puts "TOTAL TIME SPENT = #{Time.now - start_all}, for #{nof_threads} threads in parallel" | |
puts "-------------------------------------------------------\n\n" | |
end | |
__END__ | |
Running “untitled”… | |
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-darwin9.2.2] | |
Theme: | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 1.808199, for 1 process in solo | |
------------------------------------------------------- | |
task [0] took 1.776081 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 1.792507, for 1 processes in parallel | |
------------------------------------------------------- | |
task [0] took 1.966382 | |
task [1] took 2.007124 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 2.031983, for 2 processes in parallel | |
------------------------------------------------------- | |
task [1] took 2.925465 | |
task [0] took 2.972259 | |
task [2] took 2.956607 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 2.998156, for 3 processes in parallel | |
------------------------------------------------------- | |
task [1] took 3.896803 | |
task [2] took 3.925455 | |
task [0] took 3.951028 | |
task [3] took 3.986664 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 4.042733, for 4 processes in parallel | |
------------------------------------------------------- | |
task [0] took 4.978787 | |
task [4] took 4.978394 | |
task [1] took 5.015142 | |
task [2] took 5.001246 | |
task [3] took 5.048229 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 5.098819, for 5 processes in parallel | |
------------------------------------------------------- | |
task [0] took 1.811343 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 1.811851, for 1 threads in parallel | |
------------------------------------------------------- | |
task [0] took 3.670152task [1] took 3.665148 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 3.685674, for 2 threads in parallel | |
------------------------------------------------------- | |
task [2] took 5.712594 | |
task [0] took 5.794754 | |
task [1] took 5.895118 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 5.921765, for 3 threads in parallel | |
------------------------------------------------------- | |
task [0] took 9.610279 | |
task [3] took 9.71832task [1] took 9.722265 | |
task [2] took 9.741574 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 9.809111, for 4 threads in parallel | |
------------------------------------------------------- | |
task [0] took 9.586654task [2] took 9.613722task [3] took 9.630558task [1] took 9.630701 | |
task [4] took 9.662679 | |
------------------------------------------------------- | |
TOTAL TIME SPENT = 9.763632, for 5 threads in parallel | |
------------------------------------------------------- | |
copy output | |
Program exited with code #0 after 49.97 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment