簡體   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-2025 STACKOOM.COM