Ruby/Threads/sleep
Материал из Wiki.crossplatform.ru
Содержание |
Check the real interval between sleep
interval = 1.0 10.times do |x| t1 = Time.new sleep(interval) actual = Time.new - t1 difference = (actual-interval).abs percent_difference = difference / interval * 100 printf("%.9f %.6f %.6f (%.2f%%)\n",interval, actual, difference, percent_difference) interval /= 10 end
Sleep amount of time
Time.new # => Sat Mar 18 21:17:58 EST 2006 sleep(10) Time.new # => Sat Mar 18 21:18:08 EST 2006 sleep(1) Time.new # => Sat Mar 18 21:18:09 EST 2006
sleep for 1 second
100.downto(1) { |c| print c, " "; sleep 1 }
Sleep for less then a second.
puts Time.new.usec # => 377185 sleep(0.1) puts Time.new.usec # => 479230
Waiting a Certain Amount of Time
3.downto(1) { |i| puts "#{i}..."; sleep(1) }; puts "Go!"