Skip to content

Instantly share code, notes, and snippets.

@rickarubio
Created May 30, 2015 23:25
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/8a8512dc01da9e3c50d4 to your computer and use it in GitHub Desktop.
Save rickarubio/8a8512dc01da9e3c50d4 to your computer and use it in GitHub Desktop.
Order of n O(n) Example
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).round(2).to_s + ' ms'
end
def copy_string(string, number_of_times)
string * number_of_times
end
puts benchmark { copy_string('hello world', 1_000_000) }
puts benchmark { copy_string('hello world', 10_000_000) }
puts benchmark { copy_string('hello world', 100_000_000) }
# Here's an example of the output
# > ruby test.rb
# 4.45 ms
# 48.19 ms
# 486.35 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment