进度条减慢循环

Ale*_* Zh 2 julia

我已经用进度条做了一些测试,它大大减慢了测试代码的速度。有没有替代方案或解决方案?我正在寻找一种在循环时跟踪当前索引的方法,并且有一些原始方法可以在到达步骤时放置更多条件进行打印,但不是有内置的好东西吗?哦,还有一个问题,有没有办法打印从函数启动开始经过的时间并与索引一起显示?让我澄清一下,我知道@time 等,但是有没有办法计算时间并用相应的索引显示它,例如 "Reached index $i in iteration in time $time" 完成测试的代码:

function test(x)
   summ = BigInt(0);
   Juno.progress(name = "foo") do id
       for i = 1:x
           summ+=i;
           @info "foo" progress=i/x _id=id
       end
   end
   println("sum up to $x is $summ");
   return summ;
end
@benchmark test(10^4)

function test2(x)
   summ = BigInt(0);
   for i = 1:x
       summ+=i;
       (i%10 == 0) && println("Reached this milestone $i")
   end
   println("sum up to $x is $summ");
   return summ;
end
@benchmark test2(10^4)
Run Code Online (Sandbox Code Playgroud)

编辑 1

对于 Juno.progress:

BenchmarkTools.Trial: 
memory estimate:  21.66 MiB
allocs estimate:  541269
--------------
minimum time:     336.595 ms (0.00% GC)
median time:      345.875 ms (0.00% GC)
mean time:        345.701 ms (0.64% GC)
maximum time:     356.436 ms (1.34% GC)
--------------
samples:          15
evals/sample:     1
Run Code Online (Sandbox Code Playgroud)

对于粗略的简单版本:

BenchmarkTools.Trial: 
memory estimate:  1.22 MiB
allocs estimate:  60046
--------------
minimum time:     111.251 ms (0.00% GC)
median time:      117.110 ms (0.00% GC)
mean time:        119.886 ms (0.51% GC)
maximum time:     168.116 ms (15.31% GC)
--------------
samples:          42
evals/sample:     1
Run Code Online (Sandbox Code Playgroud)

pfi*_*seb 5

我建议Juno.@progress直接使用以获得更好的性能:

using BenchmarkTools

function test(x)
    summ = BigInt(0)
    Juno.progress(name = "foo") do id
        for i = 1:x
            summ += i
            @info "foo" progress = i / x _id = id
        end
    end
    println("sum up to $x is $summ")
    return summ
end
@benchmark test(10^4) # min: 326ms

function test1(x)
    summ = BigInt(0)
    Juno.@progress "foo" for i = 1:x
        summ += i
    end
    println("sum up to $x is $summ")
    return summ
end
@benchmark test1(10^4) # min 5.4ms

function test2(x)
    summ = BigInt(0)
    for i = 1:x
        summ += i
    end
    println("sum up to $x is $summ")
    return summ
end
@benchmark test2(10^4) # min 0.756ms


function test3(x)
   summ = BigInt(0);
   for i = 1:x
       summ+=i;
       (i%10 == 0) && println("Reached this milestone $i")
   end
   println("sum up to $x is $summ");
   return summ;
end
@benchmark test3(10^4) # min 33ms
Run Code Online (Sandbox Code Playgroud)

Juno.progress 不能为你做任何性能优化,但你可以手动实现它们:

function test4(x)
    summ = BigInt(0)
    update_interval = x÷200 # update every 0.5%
    Juno.progress(name = "foo") do id
        for i = 1:x
            summ += i
            if i % update_interval == 0
                @info "foo" progress = i / x _id = id
            end
        end
    end
    println("sum up to $x is $summ")
    return summ
end
@benchmark test4(10^4) # min: 5.2ms
Run Code Online (Sandbox Code Playgroud)