Info

This question is closed. Reopen it to edit or answer.

Why does Matlab check the content of an if statement, that is always false?

1 view (last 30 days)
I observed that Matlab obviously needs execution time for code that will never be used. Moreover, the performance depends on that code (that will never be used). I would like to know, why the different codes (see examples below) lead to such different execution times and how this effect can be avoided.
As a minimal example, consider the following 5 cases, which lead to the following runtimes on my machine:
  • Case1 : 0.005680 seconds.
  • Case2 : 0.029010 seconds.
  • Case3 : 0.275790 seconds.
  • Case4 : 3.849741 seconds.
  • Case5 : 5.243183 seconds.
%--------Case 1---------
tic
for k=1:10^6
if false
end
a=1;
end
toc
%--------Case 2---------
tic
for k=1:10^6
if false
svd(rand(100));
end
end
toc
%--------Case 3---------
tic
for k=1:10^6
if false
svd(rand(100));
end
a=1;
end
toc
%--------Case 4---------
tic
for k=1:10^6
if false
svd(rand(100)) %without ";"
end
end
toc
%--------Case 5---------
tic
for k=1:10^6
if false
svd(rand(100))
end
a=1;
end
toc

Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!