Jump to content
Nigel Thomas

Do local variables have a cost?

Recommended Posts

Which is more efficient - or doesn't it matter, is it just a case of readabilty:

 

var Foo: Foo;

Foo := GetFoo;
DoSomethingWithFoo(Foo);

Or:

DoSomethingWithFoo(GetFoo);

 

Share this post


Link to post
4 hours ago, Nigel Thomas said:

Which is more efficient

Hard to tell, it depends on many things and the context around it.

4 hours ago, Nigel Thomas said:

doesn't it matter

May be, again no way to tell.

4 hours ago, Nigel Thomas said:

is it just a case of readabilty

I suggest to prefer the readability always, when the efficiency is needed only then you have to test and benchmark different code/algorithms/approaches... , you can profile the code and make sure that there is wasted speed, or even better is to look at the generated assembly, but this is not everyone cup of tea.

  • Like 1

Share this post


Link to post
6 hours ago, Nigel Thomas said:

Which is more efficient - or doesn't it matter, is it just a case of readabilty:

 


var Foo: Foo;

Foo := GetFoo;
DoSomethingWithFoo(Foo);

Or:


DoSomethingWithFoo(GetFoo);

 

Does that even compile? With a variable having the same name as its type?

 

Regarding the actual question: That depends on the variable type. If it is something that can fit into a register, the variable might not even end up on the stack. But if it is something requiring additional code, e.g. reference counting, there might be a performance penalty on top of the space it needs on the stack and the code to read and write it.

 

I for one care a lot more for readability than efficiency, with very few exceptions.

Share this post


Link to post
57 minutes ago, dummzeuch said:

But if it is something requiring additional code, e.g. reference counting, there might be a performance penalty on top of the space it needs on the stack and the code to read and write it.

If it requires reference counting, then there will be hidden reference created behind the scenes and the actual generated code will be the same in both cases. 

  • Like 1

Share this post


Link to post
38 minutes ago, Dalija Prasnikar said:

If it requires reference counting, then there will be hidden reference created behind the scenes and the actual generated code will be the same in both cases. 

... unless your parameter is declared as const.

Share this post


Link to post
8 hours ago, Nigel Thomas said:

Which is more efficient - or doesn't it matter, is it just a case of readabilty:

 


var Foo: Foo;

Foo := GetFoo;
DoSomethingWithFoo(Foo);

Or:


DoSomethingWithFoo(GetFoo);

 

As has been said in other replies much depends on the specifics of the case at hand. But the first alternative has one big advantage when debugging: you can set a breakpoint on the line with the assignment and directly inspect the value of Foo there.

  • Like 1

Share this post


Link to post
11 minutes ago, dummzeuch said:

... unless your parameter is declared as const.

If Foo  is reference counted type, then GetFoo will require hidden reference created to properly initialize reference counting, regardless of how DoSomethingWithFoo is declared. If it is declared as const that only means there will be no additional reference counting involved (_IntfCopy and _IntfClear) calls.

 

Hidden reference is equivalent of the explicitly declared local variable. It is created when there is a need for holding a reference to something for calling _IntfCopy and _IntfClear methods. If there is already a reference (when passing parameter to some procedure where parameter is not declared as const) then there will be no hidden reference because _IntfCopy and _IntfClear can be called on that reference directly.

 

Same principle applies not only for interface , but also for other reference counted types like strings and dynamic arrays, the only difference is in particular reference counting methods that will be called.

Edited by Dalija Prasnikar
  • Like 3

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×