Faster computation time in for loop
1 view (last 30 days)
Show older comments
Hi all!
I am working on a project and I have run into something which I am wondering about. I have this function which assigns waiting times (random numbers from a distribution depending on a "temperature") to two arrays called WaitingTimes. I have tried to time the operation the following way:
tic
WaitingTimes(ones(1,30),ones(1,69),10);
toc
and usually the result is about 0.015, maybe 0.008 on a good run. The funny thing is that if I am instead timing it like this:
for t=1:100
tic
WaitingTimes(ones(1,30),ones(1,69),10);
toc
end
then the time is about 0.002 and 0.0065 is the highest value. Does anyone know why, and maybe if it is possible to optimize something to make my function only take 0.002 most of the time instead of 0.015?
Thanks in advance, Mads
2 Comments
James Tursa
on 20 Apr 2015
Edited: James Tursa
on 20 Apr 2015
This times involved are small enough that tic toc around a single call might not reveal much. What does this show?
tic
for t=1:100
WaitingTimes(ones(1,30),ones(1,69),10);
end
toc
Navaneeth Raman
on 21 Apr 2015
Hi,
The small difference in time shown by tic toc is expected. You machine is running a lot of other applications in the background in addition to MATLAB. This is one of the very many reasons on what is causing the time difference each time you run your function.
HTH, Navaneeth
Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!