Skip to content

Instantly share code, notes, and snippets.

@narath
Last active December 11, 2015 21:48
Show Gist options
  • Save narath/4665336 to your computer and use it in GitHub Desktop.
Save narath/4665336 to your computer and use it in GitHub Desktop.
Step timer that predicts how long something will take to complete based on measurements of how long each step is taking you
#!/usr/bin/env ruby
# provides a basic timer, and predicts how long something will take you based on how long each individual step is taking you.
# Parameters:
# timer [num_steps=100] [at_steps]
def main
total_steps = 100;
at_step = 1
if ARGV.count>0
total_steps = ARGV[0].to_i
if ARGV.count>1
at_step = ARGV[1].to_i
end
end
num_steps = total_steps-at_step+1
puts "Number of steps: #{total_steps}"
puts "Starting at step #{at_step} (#{num_steps} remaining)"
puts ""
puts "At 30 seconds per step,"
estimate_total_time([30],num_steps)
sec_per_step = []
puts "Press enter to start timing"
STDIN.gets
done = false
while !done do
start = Time.now
puts "STEP #{at_step + sec_per_step.count}"
puts "Press enter to stop timing, b to take a break (ie. stop timer, dont count), enter exit, x or q to quit"
c = STDIN.gets
if (c =~ /^b/i)
puts "Taking a break! Press enter to restart"
STDIN.gets
else
stop = Time.now
diff = (stop - start).round
puts "That took you #{diff} seconds"
sec_per_step << diff
estimate_total_time(sec_per_step, num_steps)
puts "*******************************************************"
done = (c=~/^exit|^x$|^q$/i)
end
end
puts "AlL DONE!"
summary(sec_per_step, num_steps)
end
def mean(sec_per_step)
sec_per_step.inject(0,:+) / sec_per_step.count
end
def estimate_total_time(sec_per_step, num_steps)
# mean or median could be useful
# lets start with mean
steps_remaining = num_steps - sec_per_step.count
estimated_sec_remaining = steps_remaining * mean(sec_per_step)
puts "#{steps_remaining} steps should take you #{sec_to_minutes_and_seconds(estimated_sec_remaining)}"
estimated_sec_remaining
end
def sec_to_minutes_and_seconds(sec)
hours = (sec.to_i / 3600 )
minutes = (sec.to_i % 3600 / 60)
seconds = (sec.to_i % 3600 % 60)
if hours>0
"#{hours} hours, #{minutes} minutes and #{seconds} seconds"
else
"#{minutes} minutes and #{seconds} seconds"
end
end
def summary(sec_per_step, num_steps)
puts "You did #{sec_per_step.count} steps of #{num_steps} (#{sec_per_step.count * 100 / num_steps} %)"
puts "Mean time per step = #{mean(sec_per_step)}"
puts "Max time = #{sec_to_minutes_and_seconds(sec_per_step.max)}"
puts "Min time = #{sec_to_minutes_and_seconds(sec_per_step.min)}"
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment