Solved

optimize C# code

Posted on 2003-11-13
5
739 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

13 Experts available now in Live!

Get 1:1 Help Now