How to reduce the execution time of the given piece of code?
Mostrar comentarios más antiguos
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 comentarios
Dyuman Joshi
el 5 de En. de 2024
Without the code it is (almost) impossible for us to suggest anything.
"..., it gives me an error like below. Why it is so"
The error is pretty clear about why that happens.
Errors in MATLAB are a great feature, not only do they state where the error occurs, they also state why the error occurs and sometimes provide info on how to solve them. You should read them.
There's no real way to know if or how you can make a bit of code faster without knowing what the code is. The changes that could be made might be anything from very small subtle things to large changes to a workflow. I can understand if you're reluctant to reveal certain things, but without details, the advice that can be given is very limited.
As to why the vectors change in length, my guess is that there's something going on that's causing values to be assigned to rows/columns beyond Runs,dim respectively. Something like
x = zeros(1,10) % only 10 elements
x(12) = 3 % assignment beyond the end of the array causes expansion if the expansion is unambiguous
If that's happening a lot within the loop, the expansion might be one thing that contributes to an increased execution time. As mentioned, it's unclear if there are others.
Sadiq Akbar
el 5 de En. de 2024
Editada: Torsten
el 5 de En. de 2024
Voss
el 5 de En. de 2024
@Sadiq Akbar: You should
plot(Noise,one)
instead of
plot(Noise,onev)
because onev has Runs elements whereas one has numel(Noise) elements.
Sadiq Akbar
el 6 de En. de 2024
Voss
el 6 de En. de 2024
"This will not reduce my execution time."
That's true. That's why I put it as a comment and not an answer. My intent was to explain the error you encountered and to propose an error-free alternative.
By the way, I imagine Torsten edited your comment to fix the code formatting.
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
el 7 de En. de 2024
Respuestas (1)
Taylor
el 8 de En. de 2024
1 voto
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
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!