Skip to content

Commit

Permalink
Speed up test/operator.jl, fix bug on Julia 1.0 with Base.warn_one
Browse files Browse the repository at this point in the history
Operator warning used Base.warn_once, which doesn't exist on Julia 1.0.
Test was unnecessarily expensive to basically just check one function wasn't
called too many times

Comment out all other includes in test/runtests.jl, and in test/operator.jl
comment out extension test.

```
Before
real	2m6.533s
user	2m5.312s
sys	0m1.349s

After
real	1m55.578s
user	1m54.548s
sys	0m1.176s
```
  • Loading branch information
IainNZ committed Sep 9, 2018
1 parent 270141c commit a814c83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/JuMP.jl
Expand Up @@ -724,14 +724,16 @@ function operator_warn(::AbstractModel) end
function operator_warn(model::Model)
model.operator_counter += 1
if model.operator_counter > 20000
Base.warn_once(
Compat.@warn(
"The addition operator has been used on JuMP expressions a large " *
"number of times. This warning is safe to ignore but may " *
"indicate that model generation is slower than necessary. For " *
"performance reasons, you should not add expressions in a loop. " *
"Instead of x += y, use add_to_expression!(x,y) to modify x in " *
"place. If y is a single variable, you may also use " *
"add_to_expression!(x, coef, y) for x += coef*y.")
"add_to_expression!(x, coef, y) for x += coef*y.", maxlog=1)
# NOTE: On Julia 1.0 (at least), maxlog=1 does not work correctly.
# See https://github.com/JuliaLang/julia/issues/28786.
end
end

Expand Down
26 changes: 17 additions & 9 deletions test/operator.jl
Expand Up @@ -318,15 +318,23 @@ function operators_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType::
@test dot(floats, anys) == 10 + 40 + 2x
end

@testset "JuMP PR #943" begin
pull943 = ModelType()
@variable(pull943, x[1 : 10^6]);
JuMP.set_start_value.(x, 1 : 10^6)
@expression(pull943, testsum, sum(x[i] * i for i = 1 : 10^6))
@expression(pull943, testdot1, dot(x, 1 : 10^6))
@expression(pull943, testdot2, dot(1 : 10^6, x))
@test JuMP.value(testsum, JuMP.start_value) JuMP.value(testdot1, JuMP.start_value)
@test JuMP.value(testsum, JuMP.start_value) JuMP.value(testdot2, JuMP.start_value)
if ModelType <: Model
# Only `Model` guaranteed to have `operator_counter`, so
# only test for that case.
@testset "JuMP PR #943" begin
pull943 = ModelType()
@test pull943.operator_counter == 0
@variable(pull943, x[1:100])
JuMP.set_start_value.(x, 1:100)
@expression(pull943, testsum, sum(x[i] * i for i in 1:100))
@expression(pull943, testdot1, dot(x, 1:100))
@expression(pull943, testdot2, dot(1:100, x))
@test pull943.operator_counter == 0
testadd = testdot1 + testdot2
@test pull943.operator_counter == 1 # Check triggerable.
@test JuMP.value(testsum, JuMP.start_value) JuMP.value(testdot1, JuMP.start_value)
@test JuMP.value(testsum, JuMP.start_value) JuMP.value(testdot2, JuMP.start_value)
end
end
end
end
Expand Down

0 comments on commit a814c83

Please sign in to comment.