- Your algorithm might have different number of operations, such as if-else statement.
- 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.
Change time function tic, toc
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Fangjun Jiang
am 17 Aug. 2011
It does happen and there might be reason for it.
0 Kommentare
Weitere Antworten (5)
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.
0 Kommentare
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?
0 Kommentare
tou
am 17 Aug. 2011
2 Kommentare
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
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.
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.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!