Skip to content

Instantly share code, notes, and snippets.

@RobertCam
Created October 1, 2015 18:04
Show Gist options
  • Save RobertCam/b7014434606f590288da to your computer and use it in GitHub Desktop.
Save RobertCam/b7014434606f590288da to your computer and use it in GitHub Desktop.
benchmark method using yield to pass block
def benchmark
start_time = Time.now
yield
end_time = Time.now
run_time = end_time - start_time
end
long_string = "apple"*100000000
stupid_math = 1 + 1 * 1000 - 54 * 345
running_time = benchmark { long_string.reverse }
running_time2 = benchmark { stupid_math }
puts "string.reverse took #{running_time} seconds to run"
puts "stupid math took #{running_time2} seconds to run"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment