这种方法不起作用,因为加入变量" a"不是原子操作因此不是线程安全的:
require "peach"
t=(1..500000).map{ rand};
a=0;
t2=t.pmap(4) do |z|
a+=1;
if a % 10000==0 then
print "#{a} ";
print 13.chr;
end;
Math.sin(3.14*z);
end
同样无法完美运作,计数器有时会反弹:
require "peach"
t=(1..500000).map{ rand};
t2=t.collect.with_index.pmap(4) do |z,i|
if i % 10000==0 then
print "#{(100*(i.fdiv t.size)).round(2)}";
print 13.chr;
end;
Math.sin(3.14*z);
end;
制定进度指标的任何其他方法,最好是百分比,不同于打印"。"或"#"?
答案 0 :(得分:0)
您是否考虑过使用ProgressBar gem?
高度可配置。
您应该可以执行以下操作。
pb = ProgressBar.create(:title => "Items", :starting_at => 10, :total => 100, :format => '%a %B %p%% %t')
pb.increment
这会给你一个类似下面的输出。
Time: --:--:-- 0% Progress