different time executions........
2 次查看(过去 30 天)
显示 更早的评论
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
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
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
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!