Solved

optimize C# code

Posted on 2003-11-13
5
742 Views
Last Modified: 2007-12-19
Hi,
I'd like to optimize my C# code. The following code is I want to be optimze, how to do that:

1. private ArrayList stringA = new ArrayList();
2. private string sStringTemp <== sow I use StringBuffer? and what is best for speed?
3. int iLastIndex = subString.LastIndexOf(',');
4. Use switch or if statement is better?
5  for (int i= 0; i <1000; i++) <== should I use register for i?
6. for (int i=-; i < a.Count; i++)
   should I change it to:
  j = a.Conut;
 for (int i=-; i < j; i++)
7. finalString = subString.Substring(0,subString.Length);  <== what is the best way to copy the substring?

Thanks!
jpc
0
Comment
Question by:jpchen
5 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 9740711
This question seems incomplete.

Are there 7 different questions here, or is this one chunk of code that needs optimizing?
This also looks a lot like homework.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9740863
Give more details what you need: what is the code expected to do, etc. There are a lot of errors in this code like "for (int i=-;" which means nothing
0
 
LVL 10

Accepted Solution

by:
smegghead earned 250 total points
ID: 9741051
Hi,

If you are experiencing problems with your code, you should optimise the frequently called sections of code. i.e. there's no point in optimising a piece of code that is called once or twice.

You have to get the balance of readability & structure of your code, with performance. If your code is structured well and is readable, you will find it easier to develop and enhance, whereas if you make it 0.1% more efficient by using various techniques which add more complex to code your program, then you will probably get stuck somewhere down the line where you or someone else doesn't understand why you've used an optimisation.

The following link gives some advice on optimisation.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftips.asp

HTH

Smg.
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9745528


ole Smeggy is correct.  If a piece of code is called exactly once, there is no signifigant difference between

a piece of code taking 2 ms or 2.5 ms



However, I can tell you that

string myStr = String.Empty;

is Faster Than

string myStr = "";


The Switch Statement is faster than several nested if statements, but a simple If is faster than a switch.

bool retVal = (x>y)?true:false;

is faster than

bool retVal = false;
if (x>y)
  retVal = true;


but not by much...

for (int i= 0; i <1000; i++)  // This statement simply sets i as a variable local to the loop


0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9750723
Often the best optimization is to change the algorithm. Avoid loops - especially repeated loops over the same items. To write the fastest code you would have to tell us what the code needs to do.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now