I am writing some codes in which the calculation time is important. I use tic toc function and profiler to measure the time. what is the difference between them?
for a piece of my code, tic toc function states that for example the time is 3 sec but the sum of times in profiler for all lines of my code is not that much!
The profiler is great for finding bottlenecks in your code, and for comparing relative timings for different algorithms, but I wouldn't trust it to give you accurate absolute timings.
For one thing, using the profiler disables many JIT optimisations, so the code may well not run in the same way as it does normally.
Taking timings with tic and toc can be quite subtle, as you need to warm up the code and run it a few times to take an average.
I would recommend you use timeit instead, which handles all of these subtleties for you. In recent versions timeit is part of core MATLAB. If you're using an older version, you can download it from the MATLAB Central File Exchange.
TIC TOC and profiler do very different things.
TIC TOC measures the elapsed time from the call of TIC to the call of TOC. This includes some overhead within the tic functions themselves and for very sorry intervals, is not totally reliable.
Profiler measures the cpu time for the execution of each function within your code. This does not include downtime during which no function is currently executed (the cpu is performing other tasks, including running your matlab code).
There are other things you should do to ensure a precise calculation of time:
Wrap the code in a function to ensure it is JIT compiled.
Warm up the function by running it once prior to profiling.
Run the code multiple times and average the times.
Run the tic/toc function many times, average the result, and subtract that from your total time.
If possible, allocate arrays outside of any loops, rather than appending to an array.
Related
My code is quite a long one and I want to dissect each section and loop to figure out the computational time, easy task using tic toc but can I adjust timeit() in a similar way, and the examples in matlab documentation regarding timeit() are too simple to fit in an average solution.
I have implemented a Naive Bayes classifier. On Matlab, my classify function takes 2 minutes to run while octave takes 25 minutes to run the same code. Does anyone know what causes ocatve to run slower so that I can tweak my code accordingly?
PS: I have to submit to a server which runs octave and not Matlab.
Matlab does a lot of "hidden" optimization when running your code (Octave probably, too, but different ones). Many of these optimizations e.g. concern that parameters to functions are not copied if you do not modify these parameters in the function, but instead passed by reference. This can significantly speed up calculations when you e.g. pass around large matrices, since otherwise most of your computational time is spend on copying. There are many, many similar optimizations, and not all of them are documented at all.
Without specific knowledge of what you are computing, it's hard to guess where the difference comes from. I am not aware if octave has an equivalence to the matlab profiler, but if, I would use this to find out where octave spends all the time. For debugging, I would also recommend to download Octave to your PC and debug there.
I'm having a trouble doing MCMC(Monte Carlo Markov Chain). So for MCMC, say I will run 10000 iterations, then within each iteration, I will draw some parameters. But in each iteration, I have some individual data which are independently, so I can do parfor. However, the problem is, it seems the time to finish one iteration just grows quickly as MCMC goes on. Soon, it's extremely time consuming.
My question is: is there any efficient way to combine parfor and while loop?
I have the following pseudo-code:
r=1;
while r<10000
parfor i=1:I
make draws from proposal distribution
calculate acceptance rate
accept or reject current draw
end
r=r+1;
end
Launching lots of separate parfor loops can be inefficient if each loop duration is small. Unfortunately, as you are probably aware, you cannot break out of a parfor loop. One alternative might be to use parfeval. The idea would be to make many parfeval calls (but not too many), and then you can terminate when you have sufficient results.
This (fairly long) blog article shows an example of using parfeval in a situation where you might wish to terminate the computations early.
I'm doing simulation in Matlab where some data from the simulation are obtained by executing another software. The idea is when the calculation time is beyond a limit the data from the simulation will not be accepted. How to set a maximum calculation time to automatically stop this unnecessary calculation? I don't use Simulink at the moment.
Thanks in advance!
You can try investigating the use of tic, toc commands if you are doing loop-styled calculations. e.g. at the very start of the calculation:
tic;
just before end of each loop:
if (toc>60) %//However many seconds you want
break;
end
This might not be useful if you don't have loop-style calculations you have direct access to.
If I assume that a problem is a candidate for parallization e.g. matrix multiplication or some other problem and I use an Intel i7 haswell dualcore, is there some way I can compare a parallel execution to a sequential version of the same program or will matlab optimize a program to my architecture (dualcore, quadcore..)? I would like to know the speedup from adding more processors from a good benchmark parallell program.
Unfortunately there is no such thing as a benchmark parallel program. If you measure a speedup for a benchmark algorithm that does not mean that all the algorithms will benefit from parallelization
Since your target architecture has only 2 cores you might be better off avoiding parallelization at all and let Matlab and the operative system to optimize the execution. Anyway, here are the steps I followed.
Determine if your problem is apt for parallelization by calculating the theoretical speedup. Some problems like matrix multiplication or Gauss elimination are well studied. Since I assume your problem is more complicated than that, try to decompose your algorithm into simple blocks and determine, block-wise, the advantages of parallelization.
If you find that several parts of your algorithms could profit from parallelization, study those part separately.
Obtain statistical information of the runtime of your sequential algorithm. That is, run your program X number of times under similar conditions (and similar inputs) and average the running time.
Obtain statistical information of the runtime of your parallel algorithm.
Measure with the profiler. Many people recommends to use function like tic or toc. The profiler will give you a more accurate picture of your running times, as well as detailed information per function. See the documentation for detailed information on how to use the profiler.
Don't make the mistake of not taking into account the time Matlab takes to open the pool of workers (I assume you are working with the Parallel Computing Toolbox). Depending on your number of workers, the pool takes more/less time and in some occasions it could be up to 1 minute (2011b)!
You can try "Run and time" feature on MATLAB.
Or simply put some tic and toc to the first and end of your code, respectively.
Matlab provides a number of timing functions to help you assess the performance of your code: go read the documentation here and select the function that you deem most appropriate in your case! In particular, be aware of the difference between tic toc and the cputime function.