Skip to content

Instantly share code, notes, and snippets.

@rickarubio
Created May 30, 2015 23:03
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 rickarubio/99abbbf39d3c255fe609 to your computer and use it in GitHub Desktop.
Save rickarubio/99abbbf39d3c255fe609 to your computer and use it in GitHub Desktop.
Order of 1: O(1)
require 'benchmark'
def benchmark
time_in_seconds = Benchmark.realtime { yield }
time_in_milliseconds(time_in_seconds)
end
def time_in_milliseconds(time_in_seconds)
(time_in_seconds * 1000).to_s + ' ms'
end
one_million_elements = (1..1_000_000).to_a
ten_million_elements = (1..10_000_000).to_a
one_hundred_million_elements = (1..100_000_000).to_a
puts benchmark { one_million_elements[853_123] }
puts benchmark { ten_million_elements[8_853_123] }
puts benchmark { one_hundred_million_elements[92_853_923] }
# Here's an example of the output
# > ruby test.rb
# 0.0030390219762921333 ms
# 0.000693020410835743 ms
# 0.0005030306056141853 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment