Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use getfield for performance and add performance tests #325

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ function Base.setproperty!(p::T, name::Symbol, value) where T<:AbstractProgress
value = value isa ty ? value : convert(ty, value)
setfield!(p, name, value)
else
setproperty!(p.core, name, value)
setproperty!(getfield(p, :core), name, value)
end
end
function Base.getproperty(p::T, name::Symbol) where T<:AbstractProgress
if hasfield(T, name)
getfield(p, name)
else
getproperty(p.core, name)
getproperty(getfield(p, :core), name)
end
end

Expand Down Expand Up @@ -206,14 +206,19 @@ function calc_check_iterations(p, t)
return round(Int, clamp(iterations_per_dt, 1, p.check_iterations * 10))
end

# improve performance by checking if enabled before dealing with the options
function updateProgress!(p::AbstractProgress; options...)
!p.enabled && return nothing
_updateProgress!(p; options...)
end

# update progress display
function updateProgress!(p::Progress; showvalues = (),
function _updateProgress!(p::Progress; showvalues = (),
truncate_lines = false, valuecolor = :blue,
offset::Integer = p.offset, keep = (offset == 0),
desc::Union{Nothing,AbstractString} = nothing,
ignore_predictor = false, force::Bool = false,
color = p.color, max_steps = p.n)
!p.enabled && return
if p.counter == 2 # ignore the first loop given usually uncharacteristically slow
p.tsecond = time()
end
Expand Down Expand Up @@ -295,13 +300,12 @@ function updateProgress!(p::Progress; showvalues = (),
return nothing
end

function updateProgress!(p::ProgressThresh; showvalues = (),
function _updateProgress!(p::ProgressThresh; showvalues = (),
truncate_lines = false, valuecolor = :blue,
offset::Integer = p.offset, keep = (offset == 0),
desc = p.desc,
ignore_predictor = false, force::Bool = false,
color = p.color, thresh = p.thresh)
!p.enabled && return
p.offset = offset
p.thresh = thresh
p.color = color
Expand Down Expand Up @@ -329,7 +333,7 @@ function updateProgress!(p::ProgressThresh; showvalues = (),
end
flush(p.output)
end
return
return nothing
end

if force || ignore_predictor || predicted_updates_per_dt_have_passed(p)
Expand Down Expand Up @@ -358,6 +362,7 @@ function updateProgress!(p::ProgressThresh; showvalues = (),
p.prev_update_count = p.counter
end
end
return nothing
end

const spinner_chars = ['◐','◓','◑','◒']
Expand All @@ -369,13 +374,12 @@ spinner_char(p::ProgressUnknown, spinner::AbstractVector{<:AbstractChar}) =
spinner_char(p::ProgressUnknown, spinner::AbstractString) =
p.done ? spinner_done : spinner[nextind(spinner, 1, p.spincounter % length(spinner))]

function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = false,
function _updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = false,
valuecolor = :blue, desc = p.desc,
ignore_predictor = false, force::Bool = false,
spinner::Union{AbstractChar,AbstractString,AbstractVector{<:AbstractChar}} = spinner_chars,
offset::Integer = p.offset, keep = (offset == 0),
color = p.color)
!p.enabled && return
p.offset = offset
p.color = color
p.desc = desc
Expand Down Expand Up @@ -405,7 +409,7 @@ function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = f
end
flush(p.output)
end
return
return nothing
end
if force || ignore_predictor || predicted_updates_per_dt_have_passed(p)
t = time()
Expand Down Expand Up @@ -437,9 +441,9 @@ function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = f
p.tlast = t + 2*(time()-t)
p.printed = true
p.prev_update_count = p.counter
return
end
end
return nothing
end

predicted_updates_per_dt_have_passed(p::AbstractProgress) = p.counter - p.prev_update_count >= p.check_iterations
Expand Down Expand Up @@ -528,7 +532,7 @@ function cancel(p::AbstractProgress, msg::AbstractString = "Aborted before all t
end
end
end
return
return nothing
end

"""
Expand Down
44 changes: 34 additions & 10 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ for ns in [1, 9, 10, 99, 100, 999, 1_000, 9_999, 10_000, 99_000, 100_000, 999_99
end
end

# Performance test (from #171)
function prog_perf(n)
prog = Progress(n)
# Performance test (from #171, #323)
function prog_perf(n; dt=0.1, enabled=true, force=false, safe_lock=false)
prog = Progress(n; dt, enabled, safe_lock)
x = 0.0
for i in 1:n
x += rand()
next!(prog)
next!(prog; force)
end
return x
end
Expand All @@ -43,12 +43,36 @@ function noprog_perf(n)
return x
end

if !parse(Bool, get(ENV, "CI", "false")) # CI environment is too unreliable for performance tests
prog_perf(10^7)
noprog_perf(10^7)
@time prog_perf(10^7)
@time noprog_perf(10^7)
@test @elapsed(prog_perf(10^7)) < 9*@elapsed(noprog_perf(10^7))
println("Performance tests...")

#precompile
noprog_perf(10)
prog_perf(10)
prog_perf(10; safe_lock=true)
prog_perf(10; dt=9999)
prog_perf(10; enabled=false)
prog_perf(10; enabled=false, safe_lock=true)
prog_perf(10; force=true)

t_noprog = (@elapsed noprog_perf(10^8))/10^8
t_prog = (@elapsed prog_perf(10^8))/10^8
t_lock = (@elapsed prog_perf(10^8; safe_lock=true))/10^8
t_noprint = (@elapsed prog_perf(10^8; dt=9999))/10^8
t_disabled = (@elapsed prog_perf(10^8; enabled=false))/10^8
t_disabled_lock = (@elapsed prog_perf(10^8; enabled=false, safe_lock=true))/10^8
t_force = (@elapsed prog_perf(10^2; force=true))/10^2

println("Performance results:")
println("without progress: ", ProgressMeter.speedstring(t_noprog))
println("with defaults: ", ProgressMeter.speedstring(t_prog))
println("with no printing: ", ProgressMeter.speedstring(t_noprint))
println("with disabled: ", ProgressMeter.speedstring(t_disabled))
println("with lock: ", ProgressMeter.speedstring(t_lock))
println("with lock, disabled: ", ProgressMeter.speedstring(t_disabled_lock))
println("with force: ", ProgressMeter.speedstring(t_force))

if get(ENV, "CI", "false") == "false" # CI environment is too unreliable for performance tests
@test t_prog < 9*t_noprog
end

# Avoid a NaN due to the estimated print time compensation
Expand Down