How to reduce the execution time of the given piece of code?

3 ビュー (過去 30 日間)
Sadiq Akbar
Sadiq Akbar 2024 年 1 月 5 日
回答済み: Taylor 2024 年 1 月 8 日
I have an algorithm "WHO", a function "fobjNoise" and a script "main". I run the main and I get different values of fitness for Noise that is varying from 0 to 40 dB. Then I plot the fitness vs Noise graph. But there are two issues. These are:
1- This code takes too much time. I want to reduce it execution time.
2-When I do pre-allocation for the variables onev, twov etc, like below:
Runs = 50; % Number Of Times To Run The Inner Loop
onev=zeros(Runs,1);
time1=zeros(Runs,1);
two1=zeros(Runs,dim);
twov=zeros(Runs,dim);
then when I try to plot the fitness values obtained , it gives me an error like below:
>> onev=sort(onev,'descend');
>> plot(Noise,onev)
Error using plot
Vectors must be the same length.
>>
Why it is so and what to do so that it takes less time and doesn't give me the error even if I increase the no. of Runs or I vary the Noise levels from 0 to 50 or from -40 to 40 etc.?
  9 件のコメント
Torsten
Torsten 2024 年 1 月 6 日
編集済み: Torsten 2024 年 1 月 6 日
You have just reproduced my code. You have not done any changes. So I don't understand what do you mean?
I don't want to enter the discussion. I just added initialization.m and exchange.m from the file exchange that you forgot to include.
Sadiq Akbar
Sadiq Akbar 2024 年 1 月 7 日
My problem is still not resolved. However, I am happy that at least you all have tried, that's why I am thankful to all of you.

サインインしてコメントする。

回答 (1 件)

Taylor
Taylor 2024 年 1 月 8 日
The Profiler is the perfect tool for this. You will be able to see a thorough breakdown of the runtime of your code, and identify which functions are taking the most time. There are also some common ways to speed up code outlined here

カテゴリ

Help Center および File ExchangeMathematics and Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by