Solved

optimize C# code

Posted on 2003-11-13
5
759 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!

756 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