logo

How to use progress bars in Julia 📂Julia

How to use progress bars in Julia

Overview

In Julia, you can easily use a progress bar to indicate the progress of a program.

Code

ProgressMeter.jl

By placing the @showprogress macro from the ProgressMeter.jl package in a for loop, you can display the progress1.

using ProgressMeter

chi2 = []
@showprogress for n in 1:20000
    push!(chi2, sum(randn(n) .^ 2))
end

Compared to ProgressBars.jl below, the use of a macro makes the code more concise.

showprogress.gif

ProgressBars.jl

You can wrap the iterator of a for loop with the ProgressBar() function from the ProgressBars.jl package2.

using ProgressBars

chi2 = []
for n in ProgressBar(1:20000)
    push!(chi2, sum(randn(n) .^ 2))
end

Regardless of the actual task, the progress of the program is beautifully displayed. Naturally, since it only indicates which iteration the for loop is currently on, it can only provide the average execution time per iteration, not an accurate prediction of the total time required.

progress.gif

Environment

  • OS: Windows
  • julia: v1.7.3