-
-
Save nandokakimoto/d5c802d604ecdad303e7693c5d89ecd7 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 'benchmark' | |
def fibonacci(n) | |
return 0 if n == 0 | |
return 1 if n == 1 | |
fibonacci(n-1) + fibonacci(n-2) | |
end | |
def without_threads | |
(1..30).map { |i| fibonacci(i) } | |
end | |
def with_threads | |
(1..30).map do |i| | |
Thread.new { fibonacci(i) } | |
end.each(&:join) | |
end | |
puts "Without threads:" | |
puts Benchmark.measure { without_threads } | |
puts "With threads" | |
puts Benchmark.measure { with_threads } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment