473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

efficiency of declaring local variables

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[i] == 'x') {
Console.WriteLine(s.Chars[i]);
}
}
* example (creating new variable):

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

Jul 21 '05 #1
2 2110
It is generally better (for performance) to offload
frequently used member vars into local variables,
although often times the compiler optimizer does it one
better and uses registers for countres/ptrs etc. Problem
comes in when there is a call to a function/method inside
a loop and/or the body of code within the loop is just
too big and the optimizer can no longer use registers to
hold ptrs/ctrs. This is my experience from unmanaged
code; I am pretty sure that CLR is not that much
different, since it will compile your source into native
code.

Having said all this, and being more of a code
maintenance proponent than a performance freek, I would
not use local vars unless I was doing a very CPU
intensive algorithm involving nested
iterations/recursions/etc. This is because on 1 Gig plus
processors, the performace hit is practically negligable,
but readability suffers quite a bit and can lead to
stupid mistakes such as updating a local var expecting
the unerlying (original) variable to change. I have
recently done a performance measurement comparing various
access techniques/methods (OLE DB vs native vs ODBC) on a
large database. I was iterating a million or so records
within a loop and parsing a text LOB (up to 1 meg or so)
containing within each record. The code was not very
efficient, so rewrote it to make sure that the code did
not have an impact on measurements of the data base.
Guess what - no difference, maybe a few milliseconds or
so (out of several minutes) in native VC6 code, which I
credit to a pretty good VC6 optimizer and fast CPU (P4
2G).

Now if you are a true performance fanatic, you should
also change your for statment to use a previously
declared local var instead of s.Length comparison in your
for statement...

My 2 cents...
-----Original Message-----
I am wondering if anyone has any insights on the performance benefit (ordetriment) of declaring local variables instead of referencing members.Is allocating memory for a new variable more efficient than repeatedlyreferencing the member in a loop?

Maybe using a string isn't the best example, but hopefully you get theidea!
* example (referencing member):

String s = "this is a test";
for (int i=0; i<s.Length; i++) {
if (s.Chars[i] == 'x') {
Console.WriteLine(s.Chars[i]);
}
}
* example (creating new variable):

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

.

Jul 21 '05 #2
I've not tested the performance, but this is more elegant and may run faster

String s = "this is a test";
foreach (char ch in s)
if (ch == 'x')
Console.WriteLine(ch);

Regards

Ron
"Oliver Corona" <asdlklkflkjsfdjljljlwe> wrote in message
news:Xn**********************************@216.196. 97.131...
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[i] == 'x') {
Console.WriteLine(s.Chars[i]);
}
}
* example (creating new variable):

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

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Cactus | last post by:
Hi, If I got a list is it possible to declare a variable from the items in that list? Code Sample: Blob = i = 5 for listitems in Blob: i += 1 listitems = i
24
by: JKop | last post by:
Your program begins at: int main(void); Now, in main, you want to call a function. This particular function you want to call defines a local object of type Taste, and then returns this local...
2
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date; DECLARE end_date date; SET start_date =...
1
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a variable set to the name of the control to tell the...
9
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
2
by: Wysiwyg | last post by:
I was hoping to get some opinions on the efficiency of various methods of reusing the same dropdown list data. Here is the situation: Multiple panels on maintenance pages with TAB menus across...
2
by: Oliver Corona | last post by:
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...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. For example Public Test1 as String I'll get en...
1
by: Itanium | last post by:
Hi all! I'm new to .NET Platform and got some simple questions about efficiency... To put you in situation, to say that I'm involved in the writing of a complex regex based lexer for use over...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.