Solved

optimize C# code

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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