I was reading a book about Matlab ("Accelerating Matlab with GPU computing - A Primer with Examples" by Jung Suh and Youngming Kim, 2013. Chapter 1.7 Examples).
I read an example where it said that:
by using elemental calculation, matrix calculation, and function call instead of for loops, the speed can be dramatically accelerated.
In the case I read, it reduced calculation time from 50s to 0.5s.
which syntax is more "resources saving" and "faster":
switch, and (i.e. search elements in a matrix or set) etc.?
How fast is the element-wise calculation, addition, and matrix calculation etc.?
For the convince of understanding, a short example was given below:
%%% First case tic; B = 0; A = 1:1e8; for i=1:1000 B = B + A(i); end toc; %%% Second case tic; B = 0; A = 1:1e8; B = sum(A); toc; %%% Third case C = ones(1e8,1); tic; B = 0; A = 1:1e8; B = A*C; toc;
with the following timing results
Elapsed time is 0.463159 seconds.
Elapsed time is 0.501524 seconds.
Elapsed time is 0.532470 seconds.
In the above example, all three methods had different but comparable speed.