Azurewrath

Hi all,

Is there a website where I could find some optimization techniques to improve the performance of a C# code




Thanks,
Aw


Re: Visual C# General Optimization techniques

mwalts

Optimize eh Dangerous



"Rules of Optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet."

M.A. Jackson

"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity."

W.A. Wulf



But if you really must, I'd recommend getting a profiler, finding the bottlenecks of your application and only working on those. Attempting to write the highest efficiency code straight out is a waste of time, and it bothers me that there is such an emphasis on it in University.



There is a good site that talks about performence, but it might be a little technical and scattered for you (look through it a bit, the most recent page is largely about .NET 3.5 stuff)

http://blogs.msdn.com/ricom/default.aspx


Good luck,



-mwalts





Re: Visual C# General Optimization techniques

Azurewrath

Thanks man. Basically I wrote the code already. I just want to save some performance, without doing dirty stuff. Well I don't mind dirty stuff, but for instance in the scripting language, I knocked off 15 sec out of 40 by making a method inside another instead of accessing it from a class. Looks like method lookup in the classes takes lots of time.




Aw




Re: Visual C# General Optimization techniques

ahmedilyas

Explain to us what your application does and where you think it is lagging.

The obvious is to only use variables when you need to use them, dispose of objects when you are finished using them, be sure you are not hogging the Sql connections as well as only retrieving information from your DAL (Data Access Layer) ONLY when you need to, and be sure to use that efficiently.

Be sure to use the correct data types for holding your data, so don't use a string when you are wanting to hold a single character.

Avoid String parsing of possible as its messy and expensive, should you need to do it, use the StringBuilder class which is in the System.Text namespace.






Re: Visual C# General Optimization techniques

Sean Fowler

Optimisation is one of those areas where it's essential to have the right degree of pragmatism. Don't do it just for the sake of it, do it where you've identified a need for it. Also don't assume you know what's causing the code to be slow, profile it and test it.

I'll give you an example. When I first wrote my Foundation product I populated the objects using reflection. It took 55 seconds to create and populate 12,000 objects fairly simple objects. This was clearly far too slow.

I used Visual Studio's built-in profiling tool to identify the slow bits. It turned out that the biggest culprit was retrieving the custom attributes from the properties. Optimising this away took the total time down to around 2 seconds.

I could have left it there, as it was 'fast enough', but as the speed at which data is loaded is a key feature of the product I decided to keep going.

The next biggest slow-down was caused by all the SetValue reflection calls I was making. Once I optimised this away the time went down to around a second.

Further optimisations have taken the time down to around 0.7s, clearly well into the territory of the law of diminishing returns. I made these optimisations because "they were there", rather than because they were really necessary. I didn't do anything nasty though, it was all good code.

One surprise was that getting the string value of an enumeration value is very slow. I'd improved the code by using enumerations rather than string values, but it had taken the time from 0.7s back up to around 1.2s. Optimising this away got the time back down to where it was. This is what I mean about not assuming that you know know what's going to be slow, I'd expected reflection to be slow but I certainly wouldn't have guessed that about the enumerations.

Sean






Re: Visual C# General Optimization techniques

Azurewrath

Thanks guys. Really appreciated. Basically I pass half a million float elements (values between 0 and 1) to C# from the scripting language.

Btw where is the the profiling too you guys are talking about

I now see that the process in the scripting language takes 6 secs and C# takes 32 secs. I will try to narrow it down more.




Thanks again,
Aw




Re: Visual C# General Optimization techniques

Sean Fowler

This is info about the built-in profiling. Sadly it's only available in Team System, as <irony>developers in small teams never need to optimise code, just as they never need to use unit tests</irony>

There are other profiling tools around of course, but I'm afraid I don't know enough about them to recommend any.

By the way I used the instrumentation method described in that article.

When you're passing half a million floats are you making half a million calls and passing the floats one at a time If so one thing I'd try is passing a lot of floats in one go. There are a number of ways to do this, but to see if it improves the performance you could just add a load more float parameters to the method you're calling (assuming that's how you're passing them in).

If you're accessing the .Net classes via a COM wrapper that's going to slow things down considerably. Reducing the number of calls should reduce this slow-down significantly.

If you can't use profiling another technique is to comment out chunks of code to see which bits are taking most of the time. Comment out everything in the C# and see how long it takes. Then start uncommenting blocks from the start of the process onwards and that'll help you to narrow down where the performance problems lay. As I mentioned before it can be a single line that you'd never have guessed was the culprit.

Sean

Edit: I've just seen your other post, and I see you're already passing in 20 at a time. It would be useful to increase this to 40 and see what impact that has on the performance. Also, when you create the array in the C# code are you giving it the right size to start with






Re: Visual C# General Optimization techniques

Azurewrath

Thanks man. Really useful reply.

Ok I will try passing 40. As for the size of the array I also made a function to be called once from the scripting language to set the size of the array.




Thaks again,
Aw