Advertisement
Guest User

Untitled

a guest
Mar 31st, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.88 KB | None | 0 0
  1. class SimpleBenchmarker
  2.     def self.go(how_many=1, &block)
  3.         puts "------------- Benchmarking started -------------"
  4.         block_times_array = []
  5.         start_time = Time.now
  6.         puts "Start Time:\t#{start_time}\n\n"
  7.         how_many.times do |a|
  8.             print "."
  9.             block_start_time = Time.now
  10.             block.call
  11.             block_end_time = Time.now
  12.             block_time = block_end_time - block_start_time
  13.             block_times_array << block_time
  14.         end
  15.         print "\n\n"
  16.         end_time = Time.now
  17.         puts "End time:\t#{end_time}\n"
  18.         puts "------------- Benchmarking finished -------------\n\n"
  19.         puts "Result:\t\t#{end_time.round(2) - start_time.round(2)} seconds\n\n"
  20.         puts "Our runtimes from quickest to slowests are:"
  21.         puts block_times_array.sort
  22.     end
  23. end
  24.  
  25. print "How many runs will we benchmark? "
  26. userSelectedTimes = gets.chomp.to_i
  27. SimpleBenchmarker.go (userSelectedTimes) do
  28.     time = rand(0.1..1.0)
  29.     sleep time
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement