Performance of structures as function arguments
1 次查看(过去 30 天)
显示 更早的评论
Hi, I sometimes see code where structures are passed into a function and the fields are then stored in local variables, e.g.
function(data)
alpha = data.alpha [...]
Is there a good reason to do this, are the local variables accessed quicker than the structure elements or something?
0 个评论
采纳的回答
Iain
2014-10-2
Yes.
The "Good reason", is that an equation, say,
L = 1/2 * rho * Vel.^2 * area * (alpha*P1 + P0);
Is far quicker to read and write than:
Output.Lift = 1/2 * Environment.Density * Aircraft.Velocity.X.^2 * Aircraft.WingSurfaceArea * (Aircraft.Alpha * Aircraft.LiftCoefficientP(1) + Aircraft.LiftCoefficientP0)
Structures (particularly large ones, and arrays of structures) can be much slower than basic variables.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!