[Scilab-users] Matlab vs Scilab perf

Dang Ngoc Chan, Christophe Christophe.Dang at sidel.com
Fri Mar 3 09:57:27 CET 2017


Hello,

> De : users [mailto:users-bounces at lists.scilab.org] De la part de Michael Benguigui
> Envoyé : jeudi 2 mars 2017 17:40
>
> ROT_LCS_TCS =[-cos(tooth_psi_angle(i,tooth)) -sin(tool.kappa)*sin(tooth_psi_angle(i,tooth)) -
> cos(tool.kappa)*sin(tooth_psi_angle(i,tooth)); sin(tooth_psi_angle(i,tooth)) -sin(tool.kappa)*cos(tooth_psi_angle(i,tooth)) -
> cos(tool.kappa)*cos(tooth_psi_angle(i,tooth)); 0 cos(tool.kappa) -sin(tool.kappa)];
>
> Yes, in a for loop..  And in some part of code i have nested loops..

You can optimize this expression in several manners:

1. Avoid the loops.
Scilab computes on vectors, so instead of

for i =1:n; foo(i) = sin(tooth_psi_angle(i, tooth)); end

better use

foo = sin(tooth_psi_angle(:, tooth));

2. Compute only once.
cos(tooth_psi_angle(i, tooth)), sin(tooth_psi_angle(i, tooth)), sin(tool.kappa) and cos(tool.kappa)  are computed 3 times.

Better use intermediate variables and factorization,
e.g. considering tooth_psi_angle(:, tooth) is a line vector

ctpa = cos(tooth_psi_angle(:, tooth));
stpa = sin(tooth_psi_angle(:, tooth));
stk = sin(tool.kappa);
ctk = cos(tool.kappa);
sumtk = stk + ctk;
lineofones = ones(tooth_psi_angle(:, tooth));

ROT_LCS_TCS = [ctpa - stpa*sumtk ; stpa - ctpa*sumtk ; (ctk - stk)*lineofones];

You then get a  3*n matrix (n = number of angle values).

You might even try

ROT_LCS_TCS = [ [ctpa ; stpa] - sumtk*[stpa ; ctpa] ; (ctk - stk)*lineofones];

and see if it is better.

3. To be more helpful, we need to know the whole code.

HTH

Regards

--
Christophe Dang Ngoc Chan
Mechanical calculation engineer
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.


More information about the users mailing list