Tutorial :Many Methods Kill Code Speed?


I'm building an application that is seriously slower than it should be (a process takes 4 seconds when it should take only .1 seconds, that's my goal at least).

I have a bunch of methods that pass an array from one to the other. This has kept my code nice and organized, but I'm worried that it's killing the efficiency of my code.

Can anyone confirm if this is the case?

Also, I have all of my code contained in a class separate from my UI. Is this going make things run significantly slower than if I had my code contained in the Form1.cs file?

Edit: There are about 95000 points that need to be calculated, each point goes through 7 methods that does additional calculations.


Have you tried any profiling or performance tools to narrow down why the slowdown occurs?

It might show you ways that you could use to refactor your code and improve performance.

This question asked by another user has several options that you can choose from:

Good .Net Profilers


No. This is not what is killing your code speed, unless many methods means like a million or something. You probably have more things iterating through your array than you need or realize, and the array itself may have a larger memory footprint than you realize.

Perhaps you should look into a design where instead of passing the array to 7 methods, you iterate the array once, passing the members to 7 methods, this will minimize the number of times you're iterating through 95000 members.


In general, function calls are basic enough to be highly optimized by any interpreter (or compiler). Therefore these do not produce to much blow-up in run time. In fact, if wrap your problem to, say, some fancy iterative solution, you save handling the stack, but instead have to handle some iteration variables, which will not be to hard.

I know, there have been programmers who wondered why their recursive algorithms have been so slow, until someone told them not to pass array entries by value.

You should provide some sample code. In general, you should for other bottlenecks, or find another algorithm.


Just need to run it against a good profiling tool. I've got some stuff I wished only took 4 seconds - works with upwards of a hundred million records in a pass.


An Array is a reference type not a value type. Therefore you never pass the array. You are actually passing the pointer to the array in memory. So passing the array isn't your issue. Most likely you have an issue with what you do with your array. You need to do what Jamie Keeling said and run it through a profiler or even just debug it and see if you get stuck in some big loops.


Why are you loading them all into an array and doing each method in turn rather than iterating through them as loaded?

If you can obtain them (from whatever input source) deal with them and output them (whether to screen, file our wherever) this will inevitably use less memory and reduce start-up time, at the very least.

If this answer is applicable to your situation, start by changing your methods to deal with enumerations rather than arrays (non-breaking change, since arrays are enumerations), then change your input method to yield return items as loaded rather than loading an entire array.


Sorry for posting an old link (.NET 1.1) but it was contained in VS2010 article, so: Here you can read about method costs. (Initial link)

Then, if you start your code from VS (no matters, even in Release mode) the VS debugger connects to your code and slow it down.

I know that for this advise I will be minused but... The max performance will be achieved with unsafe operations with arrays (yes it's UNSAFE, but when there is performance deal, so...)

And the last - refactor your code to use minimum of methods which working with your arrays. It will improve the performance.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »