6

I have written down a function in 4 different ways and I want to time it .

Up-to now I have been doing this thing :

tic %//function 1 toc tic %//function 2 toc tic %//function 3 toc tic %//function 4 toc 

But now I want to compute the timing data for each function for (say 100 times) each and then compute the average time spent on each function. How can I do so?

Also I read somewhere that the time printed is the elapsed “wall clock” time – so it will be affected by whatever else my computer is doing whilst the MATLAB program was running.

So is there a better way of doing it ?? I have heard there is a MATLAB built in code-profiler with the command "profile on". Please can anyone suggest me the way in which I can use it?

I have also consulted the sites : Timing code in MATLAB and Profiler to find code bottlenecks.

Please suggest how to do this many times in a loop. Thanks in advance.

2
  • 1
    If you are worried about (short) processes interfering with your measurement results. It is in general a good idea to look at the median of timings rather than the mean. Commented Sep 23, 2013 at 9:28
  • I've undone your last edit, I think it muddles the focus of this Q&A, which is otherwise very good. Regarding the question in that edit (in case it's still useful): imfilter separates the kernel if possible (you can edit it to see the code), and possibly conv2 does too, not sure. In any case, the execution time here seems dominated by the image load time, which you should exclude from the timing. Commented Jul 8, 2019 at 22:46

3 Answers 3

17

The best way to time MATLAB code is to use timeit, available from the MATLAB Central File Exchange.

It was implemented by Steve Eddins, one of the senior developers at MathWorks, and it takes care of a lot of subtleties in timing your code. For example, code runs very differently when it's executed within a function rather than within a script, and it needs to have a couple of "warm-up" runs in order to take proper advantage of the JIT compiler. It will also run the code many times in a loop, and take the median.

These things are difficult to get right without knowing a fair amount about how MATLAB works under the hood, and timeit takes care of these things for you - simple applications of tic and toc do not.

Using the profiler, as other answers have suggested, is problematic as it switches off many aspects of the JIT compiler, and will not run at the same speed as it does normally. Profiler does a great job of telling you which portions of your code take a relatively large proportion of time, i.e. discovering bottlenecks, but it's not intended for giving you actually realistic timings.

Note that in the most recent version (R2013b), timeit is available as part of core MATLAB, and does not need to be obtained from the File Exchange.

For example, to time your function one with the input argument x equal to 64, you would type:

myfun = @()one(64); timeit(myfun); 

What this does is to make a function handle to your function one (which makes sure that the code is executed inside a function, important as mentioned above), then passes this function handle into timeit. The output is timeit's estimate of the time taken to execute the code.

Sign up to request clarification or add additional context in comments.

6 Comments

PS I've just taken a look through your edit containing the functions one, two etc. If later, when you're actually going to use these functions, you will require some of their outputs, you should really time them returning output arguments.
All my functions are returning the same outputs. So is it necessary to time my functions with returning outputs ? Will they then show differeces ?
If they're all returning the same output arguments then it will probably not significantly affect the relative timings - it should have the same effect on each. But just in general, try to time things in the way they will actually be used, as it's not obvious which things will be the bottlenecks.
+1: :)) Thanks I accepted your answer. By the way can you tell me how to calculate the gradient of a 2D image in matlab in a faster method? As I understand matlab's gradient command is quite inefficient. I actually found out it here : regularize.wordpress.com/2013/06/19/…
@roni, please ask that in a separate question and we'll try.
|
3

The profiler is one possibility, but it will slow down your code significantly. Alternatively you could store the toc value within your loop or after every function call.

t(i) = toc 

and then compare these values, compute the mean or whatever, like you would deal with other vectors.

12 Comments

It analyses much more than just a simple lapse of time, and already evalutes the determined time spans. But maybe you don't need any further calculations, because the profiler offers all the information you need. try it out ;)
The profiler will slow down your code because (in order to time your code line by line, and to provide other diagnostic information it gives you), it needs to turn off various aspects of the JIT compiler.
@roni Just consider the profiler to give you upper bounds. If a line is slow you can always spot it with the profiler, but sometimes things appear to be slow that can actually be sped up if it is ran without profiler. --- For what its worth, though there may be differences between running normally and with profiler on, the profiler has always led me to the right bottleneck.
@roni Sure. Think of it like this. In very old versions of MATLAB, MATLAB was purely an interpreted language, in other words it executed your code one line at a time, then moved onto the next one. In more recent versions, it looks through your code as it runs, and precompiles bits of it so they will run faster. This is called just-in-time, or JIT compilation. When you use the profiler, in order to produce its report it needs to give information on a line-by-line basis, so it needs to switch off some of this JIT functionality. (cont...)
@roni (...) Your code therefore runs slower. Not only does it run slower, but different things will slow down more than other others, so it's not a great way to compare two different things.
|
1

Using the profiler is almost as simple as tic/toc:

profile on; for i=1:N your_function() end profile viewer; 

If your 4 functions are independent and don't influence each other, you can also profile all of them in one block:

profile on; for i=1:N your_function1() your_function2() your_function3() your_function4() end profile viewer; 

The profiler will let you have a look at the processing times for each single line of code. You can either benchmark wall-clock or cpu-time, the default is cpu-time. See the profile documentation for how to change that.

EDIT: What I like about the profiler is, that it gives you a breakdown of each subfunction's processing time - hence it's a great way to spot bottlenecks in larger processes. That's probably not so much the use case here.

3 Comments

One MAJOR problem with the profiler is that it turns the JIT accelerator off.
@DanielE.Shub Yeah thanks !! Sam also explained it to me above in his post! So timeit is better or tic-toc ? And do give your commands regarding etime ?
@roni You won't need etime for timing. At best it can give you the same results as tic and toc. In general tic and toc are used for quick and dirty checks, whilst timeit can provide better results. However, if you have many lines and quickly want to see which ones take long you can use the profiler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.