Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.