Skip to content

Instantly share code, notes, and snippets.

@jvmvik
Last active January 14, 2019 03:19
Show Gist options
  • Select an option

  • Save jvmvik/d0027dee92204b3cb417021868af381d to your computer and use it in GitHub Desktop.

Select an option

Save jvmvik/d0027dee92204b3cb417021868af381d to your computer and use it in GitHub Desktop.
Simple fibonnaci benchmark written in Ruby. To help established based line..
# simple fibonnaci benchmark
#
require 'benchmark'
def fib(n)
if n == 1
1
elsif n == 2
1
else
fib(n-1) + fib(n-2)
end
end
# complexity
N = 38
puts "ruby version: #{RUBY_VERSION}"
if `uname` =~ /Linux/
puts `lscpu | grep "Model\ name"`.split(':').last.strip
end
v = nil
timing = []
10.times.each do |i|
time = Benchmark.realtime do
v = fib(N)
end
timing << time
end
puts "average time: #{timing.sum / timing.size} s"
puts "expression: fib(#{N}) = #{v}"
@jvmvik

jvmvik commented Jan 14, 2019

Copy link
Copy Markdown
Author

Digital ocean smallest droplet
ruby version: 2.5.1
Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz
average time: 4.729319591900003 s
expression: fib(38) = 39088169

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment