Why is rand faster than rand(1) ?

조회 수: 2 (최근 30일)
sunil anandatheertha
sunil anandatheertha 2020년 7월 21일
댓글: sunil anandatheertha 2020년 7월 22일
I am optimizing my monte-carlo codes and have a question on rand function. Below, i give simple codes to get a random number. All three of the below works for my problem. Looking at the elapsed times, I want to know why is rand faster than rand(1)? Although I kind of know why method 3 is fast, your comments are most welcome.
%%%%%%%%%%%%%%
% METHOD - 1
tic
for count = 1:1e7
a = rand;
end
toc
% Elapsed time is 0.383401 seconds.
%%%%%%%%%%%%%%
% METHOD - 2
tic
for count = 1:1e7
a = rand(1);
end
toc
% Elapsed time is 7.589860 seconds.
%%%%%%%%%%%%%%
% METHOD - 3
a = rand(1e7,1);
tic
for count = 1:numel(a)
aa = a(count);
end
toc
% Elapsed time is 0.139859 seconds.
%%%%%%%%%%%%%%
  댓글 수: 5
John D'Errico
John D'Errico 2020년 7월 21일
With no inputs at all, the need for input checks would seem to be lessened.
sunil anandatheertha
sunil anandatheertha 2020년 7월 22일
Thank you.

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

답변 (0개)

카테고리

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

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by