time_limit = gets.to_f * 60
start_time = Time.new
end_time = start_time + time_limit
x = 1
until Time.new >= end_time
time_left = end_time - Time.new
time_left_in_minutes = time_left / 60
puts "Minutes Left: #{time_left_in_minutes}"
end
until x == 0
puts "Time Since End of #{time_limit / 60} Minutes: #{Time.new - end_time}"
end
My computer fan spins up to full speed and my computer gets noisy running this code. Is there a better way to do this that isn’t as much work for a processor?
This is happening because you’re asking your processor to do a lot of work!
Look at your loop:
That just spins and spins and spins until the time limit is reached. Your processor just keeps going and going. (You’re also doing some things which are expensive in here – you call
Time.newtwice on each iteration, and object creation can be expensive – but if your loop was faster you’d just spin faster.)You probably need to add a delay in that loop, so that it only runs once a second, or once a minute, or something like that.
sleep 30will pause for 30 seconds, for instance.