different time executions........

조회 수: 2 (최근 30일)
pipin
pipin 2023년 7월 27일
편집: John D'Errico 2023년 9월 28일
hi, i try this code on Hp notebook i5 intel
everytime I run the codes, it gives different time executions
x=10000
a=magic(x);
b=round(rand(x,1)*100)';
%b=[15 7 3 4 9 0 9 4]
%nei numeri > di 30 portarli al minimo di b (massimo deve avere il numero in b)
%x=30
%b+(~(b>0))
%
% min(a,b)
%ris4=logical(min(a,b)).*a>x
c=a;
tic
for i=1:height(a)
for j=1:width(a)
if b(j)~=0
c(i,j)=min(a(i,j),b(j));
end
end
end
toc
tic
z=a.*(~b>0)+min(a,b);
toc
%%******************************
tic
%a = magic(columns);
%b = [15, 7, 3, 4, 9, 0, 9, 4]; % ====> 0 is important to test it
b2 = b;
b2(b2 == 0) = inf;
% Find the minimum of a or b2
minValues = min(a, b2); % What you call "c"
toc
isequal(c,z)
isequal(c,minValues)
%c
  댓글 수: 2
Ramtej
Ramtej 2023년 9월 27일
Hi
Your computer's resources are shared by all the running processes. The execution time of your code not only depends on your code but on all other processes running in the background like your OS, other applications etc.
So, the execution time is different depending on the amount of resources used by other applications.
Fabio Freschi
Fabio Freschi 2023년 9월 28일
These are the results of three runs on my MacBook
Elapsed time is 1.100368 seconds.
Elapsed time is 0.287703 seconds.
Elapsed time is 0.044146 seconds.
Elapsed time is 0.981502 seconds.
Elapsed time is 0.185009 seconds.
Elapsed time is 0.057937 seconds.
Elapsed time is 1.005467 seconds.
Elapsed time is 0.163003 seconds.
Elapsed time is 0.059886 seconds.
pretty similar results

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

답변 (1개)

John D'Errico
John D'Errico 2023년 9월 28일
편집: John D'Errico 2023년 9월 28일
And you are surprised, because? Did you xpect exactly the same amount of time taken, EVERY TIME, to within a millisecond?
Yes, it is a computer and you probably think this is a quasi-deterministic thing. But you do understand that there are different things constantly happening on that computer in the background? Certainly, don't do anything on the side.
This is why you NEVER time anything using tic and toc, at least, not if you want any kind of serious accuracy. Use other tools, like timeit, which tries to execute the code multiple times, averaging the results. It also tries to remove problems like arm up of functions, initial call function caching, etc. Even then, timeit will not always be that accurate.
Those times are within 10% or so in each case. Tic and toc won't give much better than that.

카테고리

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