Optimization Question - structs are slower but more organized, supplementary functions are slower but more organized

34 views (last 30 days)
Hi Everyone. I'm running simulations with lots of parameters that take a long time. For organized code, I create a (1) value structure that updates throughout the simulation, a (2) a parameter structure that determines how the value structure changes, and (3) supplementary functions that update the value structure with the parameters in the parameter structure. I can't analytically solve the simulation, it needs to update each step.
I've noticed that some bottlenecks result from using supplementary functions to update parameters with struct inputs. This is much "cleaner" but sometimes significantly slower. If you pass a struct into a function because it has a parameter you need to update a value, it takes longer than just updating the value. It also takes longer than not calling the function at all.
I wrote a minimum working example copied below in the code. It has functions in the script so shouldn't need anything else to run. (I know that this example could be completed in one line without for loops, it's just an example).
If anyone has ideas on how to reap the speedups from ditching the wrapper functions and structs but still maintaining organized code, I'd appreciate it!
I'm on Matlab 2020b and when I run this code as is it prints:
Testing versions:
Outer Funx / Struct... 0.59692 second
Inner Funx / Struct... 0.57595 second
None Funx / Struct... 0.19709 second
Outer Funx / Scalar... 0.06846 second
Inner Funx / Scalar... 0.04698 second
None Funx / Scalar... 0.02228 second
fprintf('\nTesting versions: \n');
iterations = 1000000;
% Value and parameter in struct
aStruct.value = 0;
aStruct.nudgeValue = 1;
outsideStruct = tic;
for i = 1:iterations
% OutsideFunction calls insideFunction
% InsideFunction updates a struct.value with struct.parameter
aStruct = outsideFunctionStruct(aStruct);
end
fprintf('Outer Funx / Struct... %.5f second\n',toc(outsideStruct));
insideStruct = tic;
for i = 1:iterations
% InsideFunction updates a struct.value with struct.parameter
aStruct = insideFunctionStruct(aStruct);
end
fprintf('Inner Funx / Struct... %.5f second\n',toc(insideStruct));
noneStruct = tic;
for i = 1:iterations
% Directly update struct.value with struct.parameter
aStruct.value = aStruct.value + aStruct.nudgeValue*randn(1);
end
fprintf('None Funx / Struct... %.5f second\n',toc(noneStruct));
% Without Struct, only scalars
value = 0;
nudge = 1;
outsideScalar = tic;
for i = 1:iterations
% OutsideFunction calls insideFunction
% InsideFunction updates a value with a parameter
value = outsideFunctionScalar(value,nudge);
end
fprintf('Outer Funx / Scalar... %.5f second\n',toc(outsideScalar));
insideScalar = tic;
for i = 1:iterations
% InsideFunction updates value with parameter
value = insideFunctionScalar(value,nudge);
end
fprintf('Inner Funx / Scalar... %.5f second\n',toc(insideScalar));
noneScalar = tic;
for i = 1:iterations
% Directly update value
value = value + nudge*randn(1);
end
fprintf('None Funx / Scalar... %.5f second\n',toc(noneScalar));
% --- Functions Necessary To Run Script ---
function value = outsideFunctionScalar(value,nudge)
value = insideFunctionScalar(value,nudge);
end
function aStruct = outsideFunctionStruct(aStruct)
aStruct = insideFunctionStruct(aStruct);
end
% The following two are the same, but one uses a struct to hold variables
function a_struct = insideFunctionStruct(a_struct)
a_struct.value = a_struct.value + a_struct.nudgeValue * randn(1);
end
function valueOutput = insideFunctionScalar(valueInput,nudgeValue)
valueOutput = valueInput + nudgeValue*randn(1);
end
  3 Comments
Bruno Luong
Bruno Luong on 27 Mar 2021
You should put the script inside a function, the JIT can make different in both cases.
So calling function is expensive. I always avoid to call functions withon a for-loop with big number of iteration for simple arithmetics operations.
The function should accept a struct and have loop inside it, then broastcast fields parameters to local worrkspace variables; then loop with simple arithmetics.
So I simply tell you what youu have observed and no proper alternative to speed it up.

Sign in to comment.

Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!