Looking for an example showing short-cirkuit (&&) superior to normal (&) operation

조회 수: 1 (최근 30일)
Basically i am just trying to figure out how much of an issue this is, speed increase-wise. But i havent been able to make an example showing why one should bother at all.
My example shows about even performance:
clear all;
clc;
%Number of random numbers generated
T = 5000000;
%Number of times experiment is repeated
N = 20;
%Timer variables for tic/toc
timer1 = zeros(N,1);
timer2 = zeros(N,1);
for j=1:N
y1 = zeros(T,1);
y2 = zeros(T,1);
x1 = normrnd(0,1,T,1);
x2 = normrnd(0,1,T,1);
x3 = normrnd(0,1,T,1);
x4 = normrnd(0,1,T,1);
%Short cirkuited loop
tic
for i = 1:T
if (x1(i)>0 && x2(i)>0 && x3(i)>0 && x4(i)>0)
y1(i) = 1;
end
end
timer1(j) = toc;
%standard loop
tic
for i = 1:T
if (x1(i)>0 & x2(i)>0 & x3(i)>0 & x4(i)>0)
y2(i) = 1;
end
end
timer2(j) = toc;
end
mean(timer1)
mean(timer2)

채택된 답변

Jos (10584)
Jos (10584) 2014년 1월 7일
In my view, the short-circuit behaviour of && and is not primarily about speed, but more about code readability and ease of code maintenance:
  • The expression A && B forces you to express A and B in full, so that both are convertible to logical scalar values.
  • The expression A & B allows for many misinterpretations, especially when they are vectors.
Example:
x = [true false] ; y = [ true true]
tf = any(x) && all(x) % easy to interpret
tf = x & y % ??

추가 답변 (1개)

Jacob Halbrooks
Jacob Halbrooks 2014년 1월 7일
The short-circuiting behavior of && is occasionally useful for performance optimization. If you use more intensive conditions, you'll likely see some effect. Here's an example:
T = 100;
q = zeros(1000,1);
tic
for i = 1:T
if (all(q == 1) && (numel(q) > 100))
end
end
toc;
tic;
for i = 1:T
if (all(q == 1) & (numel(q) > 100))
end
end
toc;
It's sometimes valuable to optimize code by structuring the order of conditions according to likelihood and expense. For example, a quick check that often returns false should be evaluated first in a && expression.
Another aspect of short-circuiting that makes it valuable is that it allows you to write defensive code to check for the presence of a valid variable to test. For example, let's assume you have a variable v that is intialized to empty but may be set to an object at some point. You can use short-circuiting to write conditional checks like:
if ~isempty(v) && v.DoSomethingFlag
  댓글 수: 1
Matt J
Matt J 2014년 1월 7일
편집: Matt J 2014년 1월 7일
Another aspect of short-circuiting that makes it valuable is that it allows you to write defensive code to check for the presence of a valid variable to test.
You don't really need explicit short-circuiting for this. IF statements always reinterpret & as && as necessary. For example, these commands give no errors,
v=[];
if ~isempty(v) & v.DoSomethingFlag,
disp 'never reached',
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by