# JuMP solve\_time not matching actual time

**URL:** <https://discourse.julialang.org/t/jump-solve-time-not-matching-actual-time/126397>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization, gurobi, solver\
**Created:** [February 27, 2025, 5:18pm UTC](https://discourse.julialang.org/t/jump-solve-time-not-matching-actual-time/126397 "2025-02-27T17:18:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BMI\_OR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmi_or/32/213496_2.png) [@BMI\_OR](https://discourse.julialang.org/u/BMI_OR)\
**Post date:** [February 27, 2025, 5:18pm UTC](https://discourse.julialang.org/t/jump-solve-time-not-matching-actual-time/126397/1 "2025-02-27T17:18:01Z")

</div>

I am executing an algorithm that iteratively executes the following function to solve a problem:

```julia
function solve_subproblem(model, z, q, v)
    fix.(model[:z_copy], z)
    fix.(model[:q_copy], q)
    fix.(model[:v_copy], v)
    optimize!(model)
    is_solved_and_feasible(model; dual = true)
    return (
        obj = objective_value(model),
        solve_time = solve_time(model),
        z_rc = reduced_cost.(model[:z_copy]),
        q_rc = reduced_cost.(model[:q_copy]),
        v_rc = reduced_cost.(model[:v_copy]),
    )
end

```

Every iteration the values of z, q, v are different, and the model is always the same one.  
I have noticed that the first iteration, the `optimize!()` call takes significantly longer and that is not reflected when quering it with `solve_time(model)`. I have read that it may be related to how Gurobi updates the model, but I do not know if this extra time in the first iteration can be reduced.  
I am running Julia 1.10, using JuMP v1.23.5 and Gurobi v1.6.0

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2025, 6:51pm UTC](https://discourse.julialang.org/t/jump-solve-time-not-matching-actual-time/126397/2 "2025-02-27T18:51:36Z")

</div>

`solve_time` reports the time spent inside the solver, as measured by the solver itself.

The total time in `optimize!` may be longer, for example, if we need to copy the problem from JuMP into the solver, perform any reformulations, etc.

There’s also some Julia compilation latency on the first solve. Try updating to Gurobi.jl v1.7.0. I pushed a few changes recently that might make the latency smaller.

> is\_solved\_and\_feasible(model; dual = true)

Unrelated, but this isn’t doing what you think it is doing. It’s just returning `true` or `false`. But if `false`, nothing happens.

JuMP v1.24.0 introduces `assert_is_solved_and_feasible(model; dual = true)`, which will error if the return is `false`.

---

<div class="post-metadata">

**Author:** ![BMI\_OR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmi_or/32/213496_2.png) [@BMI\_OR](https://discourse.julialang.org/u/BMI_OR)\
**Post date:** [February 27, 2025, 9:03pm UTC](https://discourse.julialang.org/t/jump-solve-time-not-matching-actual-time/126397/3 "2025-02-27T21:03:56Z")

</div>

I upgraded the packages and the first `optimize!()` does not seem faster. Is there a way of pre-process the elements that apparently only happen during the first call (e.g., copy the problem from JuMP into the solver, etc.)?  
In the following iterations the durations of `optimize!()` and `solve_time(model)` are nearly identical.

\*true: `is_solved_and_feasible(model; dual = true)` is useless in this case.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 27, 2025, 9:33pm UTC](https://discourse.julialang.org/t/jump-solve-time-not-matching-actual-time/126397/4 "2025-02-27T21:33:40Z")

</div>

How much longer is the first call?

Why is it a problem if `solve_time` does not match the time spent in `optimize!`?

You can remove the overhead of JuMP copying the problem by using direct mode: [Models · JuMP](https://jump.dev/JuMP.jl/stable/manual/models/#Direct-mode)

```julia
model = direct_model(Gurobi.Optimizer())

```
