繁体   English   中英

每 x 百分比迭代显示消息 Ruby

[英]Show message every x percent iterations Ruby

我希望我的代码每处理 20% 的项目就显示一条消息。 我对自己的做法不是很满意。 我肯定有更聪明的方法

我这样做的方式:

count = 0
size = foo.get_items_size (items)
target = 20
loop all items
   items_processed = count*100 / size
   if items_processed == target
      puts "#{items_processed}% items"
      target += 20
   end
   count +=1
end



您可以通过with_index将计数器合并到您的循环中,并通过>=而不是==将当前百分比与您的目标进行比较,从而使您的代码更加健壮:

items = ('A'..'M').to_a
target = 20

items.each.with_index(1) do |item, index|
  puts item # process item

  if index.quo(items.size) >= target.quo(100)
    puts "#{target}% processed"
    target += 20
  end
end

每当超过该百分比时,这将打印 20%、40%、60% 等:

A
B
C
20% processed
D
E
F
40% processed
G
H
60% processed
I
J
K
80% processed
L
M
100% processed

请注意,实际百分比为 23%、46%、62% 和 85%。

当当前index是 20 的倍数时,我会结合使用Enumerable#each.with_index和仅打印 output:

process_steps = (1..5).map { items.size / _1 }

items.each.with_index(1) do |item, index|
  # do stuff with item

  puts "#{index}% items processes" if process_steps.include?(index)
end

检查以下代码是否有帮助:

items = 1..100
target_percent = 20
step = ((target_percent/100.0) * items.size).round

items.each.with_index(1) do |item, index|
  # Some other working logic
  puts "#{index}% of work done!" if index.modulo(step).zero?
end

由于我不知道项目列表中有什么,所以我只是将其视为一个数组。 如果您想更改target_percent ,这也将起作用。

each_slice可能是您正在寻找的:

items = (1..30)

slice_size = items.size/(100/20)
processed = 0
items.each_slice(slice_size) do |slice|
  processed += slice.size
  slice.each do |item|
    puts item #process item
  end
  puts "#{processed} from #{items.size} items processed"
end

我会用切片和索引来做:

items=(1..23).to_a
step=20.0/100
steps=(items.length*step).round
items.each_slice(steps).with_index{|slice, ind|
    puts "Accurate: #{((steps*ind)/items.length.to_f*100).round}% processed, now processing: #{slice[0..2]}..#{slice[-3..-1]}" 
    puts "Dumb down: #{(ind/(1.0/step)*100).round}% processed"
    puts 
}   
#puts "Done!"

每 20% 的报告意味着处理 5 个切片,如果项目的长度不能被 5 整除,那么最后一个切片可能是余数。

您可以准确报告进度(22%、43%、65%、87%,共 5 个桶,共 23 个项目)或将其降低到 20%、40%、60%、80%。 我在这里展示了两者。

示例打印:

Accurate: 0% processed, now processing: [1, 2, 3]..[3, 4, 5]
Dumb down: 0% processed

Accurate: 22% processed, now processing: [6, 7, 8]..[8, 9, 10]
Dumb down: 20% processed

Accurate: 43% processed, now processing: [11, 12, 13]..[13, 14, 15]
Dumb down: 40% processed

Accurate: 65% processed, now processing: [16, 17, 18]..[18, 19, 20]
Dumb down: 60% processed

Accurate: 87% processed, now processing: [21, 22, 23]..[21, 22, 23]
Dumb down: 80% processed

你也可以做常设进度百分比:

items.each_slice(steps).with_index{|slice, ind|
    printf("\r %s", "#{ind/(1.0/step)*100}%")
    sleep(0.6)
}   
puts "\rDone!     "

或者进度条:

items.each_slice(steps).with_index{|slice, ind|
    printf("\rProgress: [%-20s]", "|"*(ind*5))
    sleep(0.6)
}   
puts "\rDone!                                  "

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM