472,334 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 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 2020
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...
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...
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;...
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...
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...
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...
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. ...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.