title: Measuring execution time of many methods
body: <p>I work in project in WPF which uses Entity Framework with SQL Server. The optimalization there is crap. Everything is too slow. And I want to diagnose which parts of code kills the performance - I think that there are only few(I mean many, but not all) places which are poorly designed and kills performance.</p>
  
  <p>Now for every table we have class, for example <code>UserRepository</code>. It is not exactly the repository pattern. Those classes has methods like: <code>GetAll(...), GetById(...), GetNewest(...), GetAllWithHigherSalaryThan(int salary, int companyId)</code>, and so on, many method accessors to db. Database is used only in repository classes. </p>
  
  <p>I don't want to talk here about refactoring. I just want to measure how long does every method executes and how many times was it executed during runtime. By those information I will be able to find bugs.</p>
  
  <p>I want to measure about 100~ methods which have "selects from db" in many classes. 
  SQL Server Profiler doesn't do the trick because those methods are executed countless times and analysing logs from profiler is nightmare if possible at all with our approach to database.</p>
  
  <p>Sample method:</p>
  
  <pre><code>public IEnumerable&lt;Foo&gt; GetFoo(int y, int z)
  {
          return Context.Where(p =&gt;
              p.X == null &amp;&amp;
              p.Y == y &amp;&amp;
              p.Time &gt;= z).OrderBy(x =&gt; x.Time).AsEnumerable();
  }
  </code></pre>
  
  <p>Now I was thinking about adding stopwatch to every method, measuring execution time, counting executions and pass it to some singleton or something, then display it. Of course I will turn off this diagnostic when I'm done with it, but this approach is very week course I need to edit every method and I don't know how to turn it off after - I mean I know that I can use something like <code>#define DEBUG</code> but it is lame anyway. </p>
  
  <p>I was trying to figure something out with Reflections but with no effects... maybe there is a way to see execution time of methods in C#?</p>
  
  <p>What do you recommend? </p>