Clear Filters
Clear Filters

which has high speed performance MATLAB or C?

4 views (last 30 days)
MATLAB or C?
  1 Comment
James Tursa
James Tursa on 27 Nov 2012
Depends on what you are doing. Many of the built-in functions call optimized compiled C/C++ routines to do the actual work, so there would be no advantage to your writing such functions in C/C++ yourself.

Sign in to comment.

Accepted Answer

Jan
Jan on 27 Nov 2012
Edited: Jan on 27 Nov 2012
C.
When you are talking about the run time. But, scientists have to solve problems and the time to solve a problem is:
Time to solve = design time + programming time + testing time + debugging time ...
+ run time + time for visualization of results
Obviously the run time is only a tiny piece of the work except for rare problems like solving gigantic linear algebra equations as in a benchmark. But especially for this problem Matlab and C should use exactly the same libraries: ATLAS, MKL, etc. Then Matlab and C have the same performance.
Look at this example:
figure('name', 'My cute test');
x = rand(1, 100) * 2 * pi;
t = sort(x);
y = sin(t);
plot(t, y, 'o');
title('I am the \bf{title}\rm')
Programming time: 40 seconds, test and debug time: 10 seconds (I wrote "sin(x)" at first), run time: negligible, visualization included in programming time already.
Now do this in C, and consider that I want to run this on Macs, Linux and Windows machines, and of course this means Window from NT to 8.
Another aspect: Matlab is designed to operate on matrices. This can be inefficient compared with an elementwise operation:
x = rand(1, 1e6);
T = any(abs(x) < 0.9) ...
Now Matlab creates t1=abs(x) at first, than t2=(t1 < 0.1) and the test by any() will trigger after the first few values already (Yes, I know the ABS() is silly here). Naturally an elementwise operation would be much cheaper here:
T = false;
for ii = 1:numel(x)
if abs(x(ii)) < 0.9
T = true;
break;
end
end
But unfortunately this loop is not really fast in Matlab, when the complete array must be checked, e.g. if the values are compared with 0.000001.
In Matlab, as in any other programming language, performance is not an inherent feature of the language, but the programmer has to exploit the inner structure of problem using a matching feature of the language.

More Answers (0)

Categories

Find more on Operators and Elementary Operations 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!