Quaternions Computation Time too long
6 views (last 30 days)
Show older comments
Hello,
we have written a script to illustrate the rotation of objects in space. This is calculated once using rotation matrices and once using quaternions. We have also tracked the computation time and noticed that the computation for the quaternions take longer than for the rotation matrices. According to the theory, the calculation of quaternions should work faster than for rotation matrices. Can you explain why this is not the case here.
Thank you very much in advance.
1 Comment
Jan
on 22 Nov 2022
The relevant part of the code:
x = [0;1;0]; % Vector to rotate
theta = 33; % Angle
n = 999; % Number of rotations
Rx = rotx(theta);
tic;
for i = 1:n
y = Rx * x;
x = y;
end
toc;
y.'
% Quaternion
x = [0,1,0];
v = [1,0,0];
v_dach = quaternion(0, v(1), v(2), v(3));
theta = deg2rad(theta);
q = cos(theta/2) + sin(theta/2)*v_dach;
tic;
for i = 1:n
y = rotatepoint(q,x);
x = y;
end
toc;
y
Answers (1)
Jan
on 22 Nov 2022
Edited: Jan
on 22 Nov 2022
While the multiplication with the rotation matrix calls an optimzed BLAS library directly, rotatepoint is a function, which calls the functions prepRotate for a normalization and compact after the multiplication. Calling functions have a certain overhead. Look into prepRotate to see, that there are further checks of the inputs and function calls.
The normalization matters, if the vectors have different scalings, e.g. x = [0, 1e180, 0]. Of course considering such exceptions costs runtime. The profiler helps you to examine, where the time is spent:
profile on
% run your code
profile report
"Faster in theory" is not really meaningful, if a function is applied to tiny input data. Maybe the theoretical advantage matters, if your input data has millions of points.
0 Comments
See Also
Categories
Find more on Specialized Messages in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!