Difference between Matlab tic toc and .solvertime

98 views
Skip to first unread message

Hao Liu

unread,
May 28, 2021, 4:40:51 PM5/28/21
to YALMIP
Hi,

When I run the following code in matlab:
    tic
    a= optimize([constraints{:}], objective, options);
    a.solvertime
    toc

The a.solvertime is very different from the elapsed time matlab provides, and a.solvertime is much shorter. Could you please explain the reason and which time is the real time to solve the optimization model? Thank you very much.

Johan Löfberg

unread,
May 28, 2021, 4:50:04 PM5/28/21
to YALMIP
you are comparing the time it takes to analyze the problem and call the solvers, vs the pure solver time. those can be very different if there is large symbolic overhead (and while solvertime can be down to ms for small problems and fast solvers, the time to setup everything will never go below 0.1s or so)

Hao Liu

unread,
May 28, 2021, 4:59:50 PM5/28/21
to YALMIP
Thank you very much for your reply. According to your answer, can I understand the difference this way: the time it takes to analyze the problem is the time used to convert the constraints and objective functions to the format required by the used solver, e.g. cplex; the pure solver time is that the algorithm inside the solver, e.g. simplex, takes to find the optimal solution. Is this correct? I very much appreciate your help. 

Johan Löfberg

unread,
May 28, 2021, 5:27:12 PM5/28/21
to YALMIP
yes (except that there might be overhead in the solver too, i.e. solvertime is not only core solvertime but some solvers have bad matlab interfaces causing their code to be slow before the actual solving begins, easily seen by using a trivial optimizer . cplex appears to be roughly 2-3 times slower to get started and return

>> sdpvar x a;P = optimizer(x>=a,x,sdpsettings('solver','mosek'),a,x);
>> tic;for i = 1:1000;P(1);end;toc
Elapsed time is 1.582642 seconds.

>> sdpvar x a;P = optimizer(x>=a,x,sdpsettings('solver','gurobi'),a,x);
>> tic;for i = 1:1000;P(1);end;toc
Elapsed time is 3.116082 seconds.

>> addpath(genpath('C:\Program Files\IBM\ILOG\CPLEX_Studio1210\cplex\matlab\x64_win64'));
>> sdpvar x a;P = optimizer(x>=a,x,sdpsettings('solver','cplex'),a,x);
>> tic;for i = 1:1000;P(1);end;toc
Elapsed time is 4.946024 seconds.




Hao Liu

unread,
May 28, 2021, 7:55:41 PM5/28/21
to YALMIP
Thank you very much for the explanation. This is really helpful. 
Reply all
Reply to author
Forward
0 new messages