Skip to content

Instantly share code, notes, and snippets.

@laurenachoo
Created April 30, 2015 19:46
Show Gist options
  • Save laurenachoo/7fc0a0c09c877d84e25b to your computer and use it in GitHub Desktop.
Save laurenachoo/7fc0a0c09c877d84e25b to your computer and use it in GitHub Desktop.
def benchmark
start_time = Time.now
yield
Time.now - start_time
end
long_string = "apple"*100000000
running_time = benchmark do
long_string.reverse
end
puts "string.reverse took #{running_time} seconds to run"
# Be careful, pasting this into IRB will take a long time to print.
# It's a loooong string. :)
@monicao
Copy link

monicao commented May 4, 2015

This seems more readable.

def benchmark 
  start_time = Time.now
  yield
  end_time = Time.now
  start_time - end_time
end

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