How to benchmark Matlab processes?

前端 未结 3 1593
南笙
南笙 2020-12-15 06:33

Searching for an idea how to avoid using loop in my Matlab code, I found following comments under one question on SE:

The statement \"for loops are sl

相关标签:
3条回答
  • 2020-12-15 06:43

    The best tool for testing the performance of MATLAB code is Steve Eddins' timeit function, available here from the MATLAB Central File Exchange.

    It handles many subtle issues related to benchmarking MATLAB code for you, such as:

    • ensuring that JIT compilation is used by wrapping the benchmarked code in a function
    • warming up the code
    • running the code several times and averaging

    Update: As of release R2013b, timeit is part of core MATLAB.


    Update: As of release R2016a, MATLAB also includes a performance testing framework that handles the above issues for you in a similar way to timeit.

    0 讨论(0)
  • I think that I am right to state that many of us time Matlab by wrapping the block of code we're interested in between tic and toc. Furthermore, we take care to ensure that the total time is of the order of 10s of seconds (rather than 1s of seconds or 100s of seconds) and repeat it 3 - 5 times and take some measure of central tendency (such as the mean) and draw our conclusions from that.

    If the piece of code takes less than, say 10s, then repeat it as many times as necessary to bring it into the range, being careful to avoid any impact of one iteration on the next. And if the code naturally takes 100s of seconds or longer, either spend longer on the testing or try it with artificially small input data to run more quickly.

    In my experience it's not necessary to run programs for minutes to get data on average run time with acceptably low variance. If I run a program 5 times and one (or two) of the results is wildly different from the mean I'll re-run it.

    Of course, if the code has any features which make its run time non-deterministic then it's a different matter.

    0 讨论(0)
  • 2020-12-15 07:02

    You can use the profiler to assess how much time your functions, and the blocks of code within them, are taking.

    >> profile on; % Starts the profiler
    >> myfunctiontorun( ); % This can be a function, script or block of code
    >> profile viewer; % Opens the viewer showing you how much time everything took
    

    Viewer also clears the current profile data for next time.

    Bear in mind, profile does tend to slow execution a bit, but I believe it does so in a uniform way across everything.

    Obviously if your function is very quick, you might find you don't get reliable results so if you can run it many times or extend the computation that would improve matters.

    If it's really simple stuff you're testing, you can also just time it using tic and toc:

    >> tic; % Start the timer
    >> myfunctionname( );
    >> toc; % End the timer and display elapsed time
    

    Also if you want multiple timers, you can assign them to variables:

    >> mytimer = tic;
    >> myfunctionname( );
    >> toc(mytimer);
    

    Finally, if you want to store the elapsed time instead of display it:

    >> myresult = toc;
    
    0 讨论(0)
提交回复
热议问题