Python vs. Matlab Casadi execution time

720 views
Skip to first unread message

Nikita Zemtsov

unread,
Jun 1, 2022, 9:09:17 AM6/1/22
to CasADi
Hello,

First of all, I would like to thank you for developing and sharing the Casadi tool, I find it very useful.

I have a question on the execution time in Matlab and Python environments. I recently found out that the same function is executed approximately 5 times faster in Matlab. Below is more detail.

My approach is to design a function using 'opti.to_function' method in Matlab, save it to '.casadi' file, and load it in python with 'Function.load'.
When I am comparing the execution times in Matlab and Python I get the following results (for exactly the same inputs to the function):
Matalb:
Number of Iterations....: 17

                                   (scaled)                 (unscaled)
Objective...............: -8.0435061648940198e-001  -8.0435061648940198e-001
Dual infeasibility......:  2.5063284780912909e-014   2.5063284780912909e-014
Constraint violation....:  1.7763568394002505e-015   1.7763568394002505e-015
Complementarity.........:  3.9098818541072077e-009   3.9098818541072077e-009
Overall NLP error.......:  3.9098818541072077e-009   3.9098818541072077e-009


Number of objective function evaluations             = 18
Number of objective gradient evaluations             = 18
Number of equality constraint evaluations            = 18
Number of inequality constraint evaluations          = 18
Number of equality constraint Jacobian evaluations   = 18
Number of inequality constraint Jacobian evaluations = 18
Number of Lagrangian Hessian evaluations             = 17
Total CPU secs in IPOPT (w/o function evaluations)   =      0.049
Total CPU secs in NLP function evaluations           =      0.013

EXIT: Optimal Solution Found.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
       nlp_f  |   1.00ms ( 55.56us) 998.00us ( 55.44us)        18
       nlp_g  |   3.00ms (166.67us)   3.00ms (166.50us)        18
    nlp_grad  |   1.00ms (  1.00ms) 997.00us (997.00us)         1
  nlp_grad_f  |   3.00ms (157.89us)   3.00ms (158.16us)        19
  nlp_hess_l  |        0 (       0)        0 (       0)        17
   nlp_jac_g  |   5.00ms (263.16us)   4.98ms (262.00us)        19
       total  |  64.00ms ( 64.00ms)  63.80ms ( 63.80ms)         1

Python:
Number of Iterations....: 17

                                   (scaled)                 (unscaled)
Objective...............: -8.0434318804924654e-001  -8.0434318804924654e-001
Dual infeasibility......:  9.1038288019262836e-015   9.1038288019262836e-015
Constraint violation....:  1.7763568394002505e-015   1.7763568394002505e-015
Complementarity.........:  1.3489581364330974e-009   1.3489581364330974e-009
Overall NLP error.......:  1.3489581364330974e-009   1.3489581364330974e-009


Number of objective function evaluations             = 18
Number of objective gradient evaluations             = 18
Number of equality constraint evaluations            = 18
Number of inequality constraint evaluations          = 18
Number of equality constraint Jacobian evaluations   = 18
Number of inequality constraint Jacobian evaluations = 18
Number of Lagrangian Hessian evaluations             = 17
Total CPU secs in IPOPT (w/o function evaluations)   =      0.214
Total CPU secs in NLP function evaluations           =      0.012

EXIT: Optimal Solution Found.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
       nlp_f  |   3.00ms (166.67us)   2.96ms (164.44us)        18
       nlp_g  |   3.00ms (166.67us)   2.99ms (166.33us)        18
    nlp_grad  |   1.00ms (  1.00ms) 995.00us (995.00us)         1
  nlp_grad_f  |   4.00ms (210.53us)   3.94ms (207.63us)        19
  nlp_hess_l  |        0 (       0)        0 (       0)        17
   nlp_jac_g  |   2.00ms (105.26us)   1.99ms (104.95us)        19
       total  | 291.00ms (291.00ms) 290.90ms (290.90ms)         1

The total time is noticeably higher in Python environment (I run it a number of time and also tried different input combinations - the difference seems to be consistent: app. factor of 5).

Is it expected?
From the solver results it is visible that the major difference is in CPU secs in IPOPT (I also tried 'sqpmethod' and the different is even bigger).
Aren't casadi-Python and casadi-Matlab using the same IPOPT libraries?
Would it make any difference if the function was developed in Python environment?

Thank you in advance!
-Nikita

Leroy J D

unread,
Dec 4, 2022, 7:14:58 PM12/4/22
to CasADi
Is there an update on this question? While I haven't used the MATLAB environment, with Python I seem to be getting total times which are significantly higher than the sum of the individual wall times. This is for a problem as simple as a 2-D quadrotor over a 30-step horizon with simple Euler integration. The solve yields 50-60ms total time for a single O.L. opt using IPOPT with mumps. I thought print statements were being taken into account for wall-time but changing the verbosity level didn't affect this.
solver : t_proc (avg) t_wall (avg) n_eval
nlp_f | 0 ( 0) 0 ( 0) 18
nlp_g | 0 ( 0) 0 ( 0) 18
nlp_grad | 0 ( 0) 0 ( 0) 1
nlp_grad_f | 0 ( 0) 0 ( 0) 19
nlp_jac_g | 3.00ms (157.89us) 2.52ms (132.53us) 19
total | 60.00ms ( 60.00ms) 60.21ms ( 60.21ms) 1

Joel Andersson

unread,
Dec 5, 2022, 2:18:44 PM12/5/22
to CasADi
Hi,
I'm not sure how to explain this. The differences you show are happening in C++ code, not in the MATLAB or Python interfaces. They are not even happening in CasADi, but in IPOPT ("Total CPU secs in IPOPT (w/o function evaluations"). Are you using the same linear solver in IPOPT?  It might also be a compilation issue.

Best,
Joel
Reply all
Reply to author
Forward
0 new messages