Change time function tic, toc

6 Ansichten (letzte 30 Tage)
tou
tou am 17 Aug. 2011
I developed an algorithm that returns the execution time, to do this, I used the function tic at the beginning and t=toc; at the end. when I execute each time this algorithm, the function {tic,toc} gives different results, why? note: there is no problem on my algorithme

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 17 Aug. 2011
It does happen and there might be reason for it.
  1. Your algorithm might have different number of operations, such as if-else statement.
  2. Even the above doesn't apply and you are running the exactly same code, there might be other factors that impact the time, such as the loading of internal library, Just-In-Time optimization, etc. When I run bench, the results of second run is much better than the first run.

Weitere Antworten (5)

Paulo Silva
Paulo Silva am 17 Aug. 2011
It's just the same like you going from your house to the supermarket expecting to always take the same time up to the second, there will be always differences in seconds because of the huge number of variables involved, you could be tired and walk or drive slower, someone might delay you, etc.
Like Daniel said your computer does lots of things thus the differences you get, it doesn't do many tasks in real time.
If you want reliable timings do the same task several times, make sure no other programs are running at the same time and take the average of the timings.

Daniel Shub
Daniel Shub am 17 Aug. 2011
Your computer does lots of things besides run MATLAB. Maybe it went and checked for updates. Maybe something was hogging up RAM and the processor. Why would you expect it to take the same amount of time?

tou
tou am 17 Aug. 2011
I program 7 algorithms, and I want to compare them, what is the most speed algorithm. which is why I fall into this problem,
becaufe the execution time varies each time I execute the same function with the same data.
these responses is more convincing, but I want an answer more academic
to put in my thesis.
thank you
  2 Kommentare
Fangjun Jiang
Fangjun Jiang am 17 Aug. 2011
Maybe you can try cputime(). I am not sure if that is more accurate. I don't have experience. But in general, you'll need to have multiple run, take the measurements and calculate the mean to compare. That is one way to deal with the normal noise factor.
Daniel Shub
Daniel Shub am 17 Aug. 2011
As Paulo said, run each algorithm N times, where N is as large as you have time for, recording the time each time. Then look at the mean, median, std, etc. and do something academic like. It might be worth using cputime instead of wall time.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 17 Aug. 2011
It is not possible to determine which algorithm is the fastest by timing it with specific data. When I say "not possible", I am speaking in the Computing Theory sense. Computing Theory does not attempt to rank algorithm speeds according to specific runs: instead it talks about matters such as "best case", "average case", and "worst case", and it does those calculations in terms of the number of inputs. Computing theory is quite happy with situations such as one algorithm being faster than another up to (say) 10^10^10 inputs, and will happily say that the second algorithm that is faster after that is the "better algorithm" even though the running time to get to that cross-over point might exceed the expected remaining lifetime of the Universe.
So you've shown that with some particular set of data, on one particular run, one algorithm was 0.0037 seconds faster than another. Now what? Have you tried running the algorithms with enough data to exceed the primary and secondary cache of your hardware? Which algorithm is faster now? What happens to the speed of your algorithms if your array sizes happen to be such that you get cache-line conflicts? Have your algorithms been programmed to carefully avoid those? Do you even know what a cache-line conflict is? If you do not, then you do not know enough about benchmarking algorithms to say which of your algorithms is the fastest.

tou
tou am 20 Aug. 2011
in each algorithm, there's a loop while : function V1=funct1(f,Eps) tic ..... while(f-a <Eps) n1=n1+1; ..... end t1=toc; V1=[n1,t1]; end. function V2=funct2(f,Eps) tic ..... while(f-a <Eps) n2=n2+1; ..... end t2=toc; V2=[n2,t2]; end. Eps = 0.1,0.01, * if epsilon = 0.1 n= 3000 and t1 = 0.436 .(by funct1) * if epsilon = 0.01 n= 4050 and t1 = 0.037 .(by funct1) my exact question: when Eps change from 0.1 to 0.01 n is increases (increases is evident!!),and time must be increase (in theory), but time is fall !!, why??? (Then I say to: if t1<t2 and n1<n2 implies funct1 is the best )

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by