开发者

How to make my code fast [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this qu开发者_开发知识库estion so that it can be reopened, visit the help center. Closed 12 years ago.

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.


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.


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.


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).


Type REALLY fast.


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


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


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)


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.


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.


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜