C# Performance tips

Started by
4 comments, last by helix 18 years, 9 months ago
Does anyone know of some tips or a specific resource for writing high performance c# code? I understand about avoiding boxing/unboxing, any other fundemental things i should avoid before getting to far in? Cheers
Advertisement
Try the Microsoft article about it...

Tim
Depending on what you are doing, you might also try writing the performance critical code in C/C++ and linking it as a DLL. I did this for the second iteration of my perlin noise texture generator (source code) and I got a nice 5x-8x speedup (lots of integer math). Mileage may vary, but it was very easy to set up and get working.

Other than that, you can get the .Net 2.0 beta which has typed generics that will alleviate the boxing/unboxing problem.

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Perfomance tip #1 Get it all done, then and only then look for performance bottlenecks.

That said there's nothing wrong in follow heuristics as you go along, but it's better to write code you can understand and follow rather then sometimes brittle performance code. Then when it's done identify performance issues.

It is very easy to fall in to the premature optimisation trap... I know I have done in the past and probably will (despite knowing better) in the future.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
I don't think that's such a good performance tip. If you really get it all done and then notice that it's too slow for your needs there's a risk you might be screwed. I don't mean that you should optimize all the time, but you should definitely be aware of potential performance problems. A slow algorithm might be easy to replace but a "slow design" or general misuse of language features can be a lot harder to rework.
You both are right. It's important to design it properly up front so you have a good algorithm (etc) when you are finished that you can then optimize. But you don't want to be optimizing too much on the fly or your code will run the risk of becoming unreadable, buggy, etc. I don't really follow my own advice too well though...

This topic is closed to new replies.

Advertisement