How to get consistant results when testing code performance

2 Ansichten (letzte 30 Tage)
Jianfei
Jianfei am 17 Feb. 2025
Kommentiert: Walter Roberson am 17 Feb. 2025
I have functions to make searches in Simulink models. I want to test which algorithm has better performance. I use the "Run and Time" to check the running time. But the performance varies a lot each time. What I have done is simplely closing other software, running "bdclose all" before starting the test. Is there anything I can do to achieve a more consistant results?

Antworten (2)

Walter Roberson
Walter Roberson am 17 Feb. 2025
You could potentially code MATLAB-level sim() calls, inside a timeit
However according to https://steveeddins.com/story-of-timeit/ if the code to be timed 11 times would take more than 15 seconds, then the number of calls will be reduced, possibly down to as few as 3 times.

John D'Errico
John D'Errico am 17 Feb. 2025
A single time test will be susceptible to anything that attracts the attention of your CPU. And sadly, your CPU suffers from computational ADHD.
So run multiple tests, then take the average. Better yet, take N tests, then drop the slowest and fastest times, and take the average of the other N-2, so a trimmed mean. Or just use the median, instead of the average.
Absolutely minimize everything else that is running on your computer. DON'T check your mail during a test. Close your web browser. Make sure nothing is automatically performing a backup while the test is happening.
  1 Kommentar
Walter Roberson
Walter Roberson am 17 Feb. 2025
Better yet, close your mail program during the test, if you are using a mail application.
And your anti-virus.
And automatic disk defragmenters.
And minesweeper.
And external performance monitors.
And if you are working on MacOS, any of ~100-ish background daemons, including searchlight indexing, and Slack, and branded mouse helpers, and MATLAB Connector, and Siri, and keyboard helpers...

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by