473,395 Members | 1,474 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):

Dim s As String = "this is a test"
For i As Integer = 0 To s.Length
If s.Chars(i) = "x"c Then
Console.WriteLine(s.Chars(i))
End If
Next i
* example (creating new variable):

Dim s As String = "this is a test"
Dim chars As Char() = s.Chars
For i As Integer = 0 To s.Length
If chars(i) = "x"c Then
Console.WriteLine(chars(i))
End If
Next i

Nov 20 '05 #1
1 1272
Oliver,
I would recommend you reference the member, then if you find that has a
performance issue change it.

I rarely try to 'second guess' the VB.NET or C# compiler itself or the JIT
compiler.

Normally you should write correct straight forward programs first, then
when your routine is determined to have a performance problem, change that
routine to be better optimized.

In other words, don't waste your time optimizing a routine that you don't
even know it has a problem.
The following articles provide information on writing .NET code that
performs well.

http://msdn.microsoft.com/library/de...anagedcode.asp

http://msdn.microsoft.com/library/de...anagedapps.asp

http://msdn.microsoft.com/library/de...vbnstrcatn.asp

http://msdn.microsoft.com/library/de...tchperfopt.asp

http://msdn.microsoft.com/library/de...tperftechs.asp

Hope this helps
Jay
"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):

Dim s As String = "this is a test"
For i As Integer = 0 To s.Length
If s.Chars(i) = "x"c Then
Console.WriteLine(s.Chars(i))
End If
Next i
* example (creating new variable):

Dim s As String = "this is a test"
Dim chars As Char() = s.Chars
For i As Integer = 0 To s.Length
If chars(i) = "x"c Then
Console.WriteLine(chars(i))
End If
Next i

Nov 20 '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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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.