efficiency of declaring local variables

O

Oliver Corona

I am wondering if anyone has any insights on the performance benefit (or
detriment) of declaring local variables instead of referencing members.
Is allocating memory for a new variable more efficient than repeatedly
referencing the member in a loop?

Maybe using a string isn't the best example, but hopefully you get the
idea!


* example (referencing member):

String s = "this is a test";
for (int i=0; i<s.Length; i++) {
if (s.Chars == 'x') {
Console.WriteLine(s.Chars);
}
}


* example (creating new variable):

String s = "this is a test";
char[] chars = s.Chars;
for (int i=0; i<s.Length; i++) {
if (chars == 'x') {
Console.WriteLine(chars);
}
}
 
J

Juan Gabriel Del Cid

I am wondering if anyone has any insights on the performance
benefit (or detriment) of declaring local variables instead
of referencing members. Is allocating memory for a new
variable more efficient than repeatedly referencing the
member in a loop?

When you access a member of an object, you need to compute it's address by
adding the member's offset to the object's base address. This is one more
step than just referencing a local variable, so, you might have a little
performance gain by using a local variable.

Remember that to significantly better an algorithms performance, you have to
take into account it's Big O
(http://www.nist.gov/dads/HTML/bigOnotation.html). Basically, this means
that it doesn't matter if the algorithm takes 4 instruction each time
through the loop or 5 each time... what matter is how many times we loop,
how much data storage space we need, etc.

If you really want to better your perforance, this is what you want to think
about. Don't try to get a nanosecond's worth of improvement if you use a
local var or a member var, etc. In the long run, it doesn't matter.

Hope that helps,
-JG
 
Z

Zhanyong Wan [MSFT]

| From: "Juan Gabriel Del Cid" <[email protected]>
|
| > I am wondering if anyone has any insights on the performance
| > benefit (or detriment) of declaring local variables instead
| > of referencing members. Is allocating memory for a new
| > variable more efficient than repeatedly referencing the
| > member in a loop?
|
| When you access a member of an object, you need to compute it's address by
| adding the member's offset to the object's base address. This is one more
| step than just referencing a local variable, so, you might have a little
| performance gain by using a local variable.

On a mordern x86 computer, the adding of the offset and the accessing of
the memory can be done in one instruction, and therefore there might be no
performance difference at all.

| Remember that to significantly better an algorithms performance, you have
to
| take into account it's Big O
| (http://www.nist.gov/dads/HTML/bigOnotation.html). Basically, this means
| that it doesn't matter if the algorithm takes 4 instruction each time
| through the loop or 5 each time... what matter is how many times we loop,
| how much data storage space we need, etc.
|
| If you really want to better your perforance, this is what you want to
think
| about. Don't try to get a nanosecond's worth of improvement if you use a
| local var or a member var, etc. In the long run, it doesn't matter.

Agreed. According to Donald Knuth, premature optimization is the source of
all evil.

BTW, C# has what's called "properties", which are member functions dressed
up as member variables. In Oliver's example, s.Chars actually is a
function call (something like s.getChars()), although the syntax looks like
you are just accessing a variable. In this case, the cost for accessing
the property can be arbitrarily high -- it all depends on how the property
is implemented.

| Hope that helps,
| -JG

- Zhanyong Wan

Visual Studio and .NET Setup

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included samples (if any) is subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top