Skip to content

Instantly share code, notes, and snippets.

@joaquimg
Last active July 24, 2017 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaquimg/68c67de76eea7adea0b9ab880c667dfc to your computer and use it in GitHub Desktop.
Save joaquimg/68c67de76eea7adea0b9ab880c667dfc to your computer and use it in GitHub Desktop.
using Xpress
Solver = Xpress

using BenchmarkTools

BenchmarkTools.DEFAULT_PARAMETERS.samples = 100

const nvars = 1000
const ncons = 1000
const ncoefs = 100

const inds = collect(1:div(nvars,ncoefs):nvars)
const coefs = collect(1.0:Float64(ncoefs))
@assert length(inds) == length(coefs)

const At = sparse(repmat(inds, ncons), repmat(collect(1:ncoefs),1,ncons)'[:], repmat(coefs, ncons), nvars, ncons)
const rhs = fill(24.1,ncons)

# env = Solver.Env()
# m1 = Solver.Model(env,"model")
m1 = Solver.Model()
Solver.add_cvars!(m1, zeros(nvars))
function add1000cons1(m)
    for i in 1:ncons
        Solver.add_constr!(m, inds, coefs, '<', 24.1)
    end
end

m2 = Solver.Model()
Solver.add_cvars!(m2, zeros(nvars))
function add1000cons2(m)

    add_constrs_t!(m, At, '<', rhs)
    nothing
end

# @benchmark add1000cons1(m1)
# @benchmark add1000cons2(m2)
julia> @benchmark add1000cons1(m1)
BenchmarkTools.Trial:
  memory estimate:  1.31 MiB
  allocs estimate:  5000
  --------------
  minimum time:     1.514 ms (0.00% GC)
  median time:      2.413 ms (0.00% GC)
  mean time:        13.977 ms (0.91% GC)
  maximum time:     690.745 ms (0.00% GC)
  --------------
  samples:          100
  evals/sample:     1

julia> @benchmark add1000cons2(m2)
BenchmarkTools.Trial:
  memory estimate:  95.50 KiB
  allocs estimate:  9
  --------------
  minimum time:     180.898 μs (0.00% GC)
  median time:      216.179 μs (0.00% GC)
  mean time:        219.536 μs (0.00% GC)
  maximum time:     299.358 μs (0.00% GC)
  --------------
  samples:          100
  evals/sample:     1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment