Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up the execution of C#/.NET application [closed]

I'm searching for methods to boost up C#/.NET application. What I found so far is

  1. Use ngen.
  2. Careful when using as operator
  3. Careful when using reflection API. It takes quite a while to load dll with Assembly.LoadFrom().

What else tools/tips/good practices do you have to get the best performance out of C#/.NET program.

like image 241
prosseek Avatar asked Jun 06 '11 21:06

prosseek


2 Answers

First be careful with ngen. It might actually end up hurting performance. See Jeff Richter's book "CLR via C#, Third Edition" for more details.

Personally I will use a profiler when I need to performance tune my application. The one I prefer is Red-Gate Ants, but there are plenty of good ones on the market. However using these can be a bit tricky because they will produce a lot of noise.

Most of the problems I have seen are usually caused by the developer or overall architecture rather than the .Net Framework. For example an Asp.Net web application that requires 30+ calls to the Database just for the home page to load.

like image 193
Jeff Avatar answered Sep 18 '22 23:09

Jeff


Memoize expensive functions when possible.

public static class Expensive
{
    private static readonly ConcurrentDictionary<int, int> _map = 
        new ConcurrentDictionary<int, int>();

    public static int Calculate(int n)
    {
        return _map.AddOrUpdate(n, Add, (key, value) => value);
    }

    static int Add(int key)
    {
        return 0;
    }
}
like image 40
ChaosPandion Avatar answered Sep 18 '22 23:09

ChaosPandion