如何才能找到最准确的变化率?
这是我的情况:
我知道变化的速度是:( newamount - oldamount)/ time但是我在“时间”片段上苦苦挣扎。
这是我到目前为止所做的:
prev = ""
loop do
# Let's find the number of files in the input queue, this is a method that basically does a wc -l on a directory.
num = number_of_input_files
# Sleep if there's nothing in 0
if num.to_i == 0 or prev == ""
prev = num
puts "Current: 0"
sleep 60
next
end
# Find the difference between the current queue number and the previous queue number
sum = prev.to_i - num.to_i
puts "Current: #{num}, Previous: #{prev}, Rate #{sum} per minute"
prev = num
sleep 60
end
我想我会在睡眠中拍摄自己的脚60,但我不确定如何捕获更准确的数字。我该怎么做?
答案 0 :(得分:0)
存储脚本开始处理的Time,您可以随时查看差异。简而言之:
start = Time.now
# do work
diff = Time.now - start
如果这个脚本同时计算速率并进行处理,那么睡眠就会成为一个问题,但是由于另一个脚本正在处理,你应该没问题。
从脚本的结构来看,只要目录变空,您就会想要重置start
。您还可以计算完成的预计完成时间,在我运行这些类型的东西时,我觉得很方便。
prev = nil
loop do
# Let's find the number of files in the input queue, this is a method that basically does a wc -l on a directory.
num = number_of_input_files.to_i
# Sleep if there's nothing in 0
if num == 0 || prev.nil?
prev = num
puts "Current: 0"
start = Time.now
sleep 60
next
end
# Find the difference between the current queue number and the previous queue number
sum = prev - num
diff = (Time.now - start) / 60.0 # per minute
rate = sum / diff
remaining = sum / rate
puts "Current: #{num}, Previous: #{prev}, Rate #{rate} per minute"
puts "#{remaining} minutes remaining"
prev = num
sleep 60
end
只要将一批工作转储到目录中, sum
将在一个周期内为负,因此您可能也想要考虑到这一点。