Performance of structures as function arguments

조회 수: 1 (최근 30일)
Dan
Dan 2014년 10월 2일
댓글: Dan 2014년 10월 2일
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?

채택된 답변

Iain
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.
  댓글 수: 1
Dan
Dan 2014년 10월 2일
Ok thanks, i can see that this can be a reason. To clarify though, my interest was not in speed of writing or reading of code, but in Matlab reading structures (not arrays of structures) rather than basic variables.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by