I tried the same code with MatlabMATLAB 2011b with my MacBook Pro ( IntelIntel i7, 64 bit, 2.4 GHz) for the following matlabMATLAB code:
% Processing time with Sorted data vs unsorted data
%==========================================================================
% Generate data
arraySize = 32768
sum =0;= 0;
%generate% Generate random integer data from range 0 to 255
data = randi(256, arraySize, 1);
%Sort the data
data1= sort(data); % data1= data when no sorting done
%Start a stopwatch timer to measure the execution time
tic;
for i=1:100000
for j=1:arraySize
if data1(j)>=128
sum=sum + data1(j);
end
end
end
toc;
ExeTimeWithSorting=ExeTimeWithSorting = toc - tic;
The results for the above MatlabMATLAB code areare as follows:
a: Elapsed time (without sorting) = 3479.880861 seconds.
b: Elapsed time (with sorting ) = 2377.873098 seconds.
The results of the C code as in @GManNickG II get :
a: Elapsed time (without sorting) = 19.8761 sec.
b: Elapsed time (with sorting ) = 7.37778 sec.
Based on this, it looks MatlabMATLAB is almost 175 times
175 times slower than the C implementation without sorting andand 350 times
350 times slower with sorting. In other words, thethe effect (of branch prediction) is 1.46x
1.46x for MatlabMATLAB implementation and 2.7x
2.7x for the C implementation.