Skip to content

Instantly share code, notes, and snippets.

@matugm
Last active November 17, 2018 15:34
Show Gist options
  • Save matugm/64fe14666a216a1beaea5ef32a08ef01 to your computer and use it in GitHub Desktop.
Save matugm/64fe14666a216a1beaea5ef32a08ef01 to your computer and use it in GitHub Desktop.
Benchmarks
def a
t = Time.now
Array.new(10_000_000) { rand }
puts Time.now - t
end
10.times { a }
def a
t = Time.now
10_000_000.times { "abcdefg".upcase }
puts Time.now - t
end
10.times { a }
def a
t = Time.now
i = 1
n = 1
while i < 100_000
n *= i
n += 1
i += 1
end
n
puts Time.now - t
end
10.times { a }
def a
t = Time.now
i = 1
n = 1
while i < 10_000_000
n *= i
n %= 10000
n += 1
i += 1
end
n
puts Time.now - t
end
10.times { a }
def a
t = Time.now
i = 0
str = ""
while i < 10_000_000
i += 1
str << i.to_s
end
puts Time.now - t
end
10.times { a }
def a
t = Time.now
i = 0
while i < 10_000_000
i += 1
end
puts Time.now - t
end
10.times { a }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment