Solved

optimize C# code

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 47
How to use spatial data types in SQL Server Database project 2 62
array not updating 8 41
Inserting LocalDB Table to SQL Server C# 3 47
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

737 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