Skip to content

Instantly share code, notes, and snippets.

@nandokakimoto
Created July 24, 2017 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nandokakimoto/d5c802d604ecdad303e7693c5d89ecd7 to your computer and use it in GitHub Desktop.
Save nandokakimoto/d5c802d604ecdad303e7693c5d89ecd7 to your computer and use it in GitHub Desktop.
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