views:

541

answers:

12

Hi,

Please can anyone one point me to a good tutorial that helps me to make my code fast and light. I'm interested to know which Method is faster and when to use a method instead of other...
And how to evaluate if a code is good or bad?
My programming language is C#.

Hi all,
Thanks for your replies, they are very helpful.
I'm editing my question to be more specific specially that optimization is unlimited.
I want to know what is the best method in each condition.
For example using StringBuilder is better than string if i am appending lines to a string... I only need this simple things.

+2  A: 

If one method was always faster than another, they wouldn't bother including the slower one.

The only invariant when it comes to performance is that you need to profile. Everything follows from that.

Ken
Not true. Look at all the ways you can do file i/o in C# for example. Old methods hang around for backwards compatibility, and there are various abstraction layers depending upon how low-level or high-level you want to go.
RedFilter
+4  A: 

Type REALLY fast.

bobber205
I would add: (1) strip out all comments (2) shorten variables to a single letter (3) remove unnecessary whitespace, including newlines (4) re-use variables for different purposes... ;-)
Scott Smith
and i would add...if you don't type fast.. use a speech-to-text software:Pi can't imagine a programmer using a speech-to-text software to code...
scatman
+3  A: 

You should look at hidden features of c#, this post covers the most best practises in c# development

Cheers

Ramesh Vel
+13  A: 

Be aware of sub optimization.

Even though one specific function is faster than another replacing this isn't necessarily gonna make any difference in the runtime of your application. You need to understand which parts of your code that actually is a potential problem, and focus on optimizing these parts. Be aware of the O notation of your functions, and how often they are called. To identify parts that needs optimization a profiler can be of good help.

This question has some interesting points on why you shouldn't optimize until there is actually a need to do so.

stiank81
+5  A: 

Basically implement first, then test where to optimize.

If you are using Visual Studio Profissional you can use Analyze -> Launch Performance Wizard to analyze method performance. I am not sure about whether the other versions support this feature, however, there are also some commercial/free applications around ... look for profiler (see here for a list).

Obalix
+1  A: 

In terms of general advice:

  • Try to use the fewest loops necessary
  • Move code out of loops where possible
  • Avoid copying things (like strings) in loops
  • Avoid creating objects in loops
  • Cache where warranted (generally small objects that take a lot of time to make), but make sure your cache has a good disposal policy or it turns into a memory leak
Gabe
+2  A: 

If you get yourself a profiler, it will help you along, some will even give you great tips.

Example: ANTS Profiler

Usually you will find that reducing the number of times you create Strings to be the main performance boost you can get.

That and not messing with the Garbage Collector manually (unless you really really know what you're doing)

This link for Java design patterns is way too involved, don't get too put off by the word Java there, you can use what they teach for development in any language.

The thing is, if you want to know when to do what and what methods to use and so on, design patterns is what you are talking about.

I wish someone had pointed this to me earlier in my career.

Ric Tokyo
+1 for ANTS Profiler
Richard Ev
+5  A: 

You can get a ton on advice of on this. But be aware of : Premature optimization is root of all evil.

fastcodejava
+2  A: 

You can compile your program in native mode to improve the runtime performance.

Jojo Sardez
A: 

One of the ways of figuring this out yourself is having a console app where you try running discrete pieces of code against each other and timing them. Like here.

Eugene
+3  A: 

Aim first for correctness, next for clarity and only then performance.

As the old saying goes,

"No one cares how quickly you can calculate the wrong answer"

(on a practical level though, use a profiler)

the_mandrill
But doesn't that help QA find your bugs more quickly? :)
RedFilter
+6  A: 

Sure. Here's what we do:

  • Begin the journey by deciding when the journey is over. Set meaningful, customer-focussed, realistic performance goals. (For both speed and resource consumption.)

  • Carefully test your code frequently to see if you are meeting your performance targets.

  • If you are meeting your performance targets, don't worry about performance. Things are fine. Worry about bugs, or robustness, or features.

  • If you are not meeting your performance targets, run a profiler. Use it to identify what is the worst offending code. It only makes sense to fix the worst code; making something that is already incredibly fast and light slightly faster and lighter does not solve your performance problem.

  • Rewrite the slow code so that it's more performant. (This is the hard bit.) Make sure you test it to make sure it really is better.

  • If despite your best efforts you cannot make it good enough, either re-evaluate what your goals are, or cancel the project and spend your time on something that you can be successful at.

Keep iterating on that until you ship something.

Eric Lippert