473,385 Members | 1,384 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,385 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]);
}
}

Nov 15 '05 #1
1 1755
> 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
Nov 15 '05 #2

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
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...
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...
4
by: jhightow | last post by:
The following code : For i as system.Int32 = 0 to 4 If i < 2 Then Dim s as System.String s &= i.ToString Console.WriteLine(s) End If Next
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...

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.