tags:

views:

54

answers:

2

If I look at the il produced for my C# code (I can do this using the great LINQPad tool), what should I look for to find out what may be a performance bottleneck?

I have read the book Inside Intermediate Language to understand the code, but it doesn't really cover how to use this information to make decisions and thus changes.

Thanks

+5  A: 

Examining IL is good for narrowing down what specific lines of code are causing unintended side effects, but is probably less useful for tracking down a performance bottleneck.

I'd recommend using a profiler for that purpose instead. Ideally, run a sampling and a tracing profiler (if it's very important). Once you've found your bottlenecks in a profiler, you could look at the IL to help you fix the problems.

Typically, however, the profiler results highlight the problems and it's fairly obvious what needs changing. If the code isn't obvious, examining the IL itself can help. The only place where I've found this useful is in code being run many times (in a tight loop, for example). You can compare 2 different approaches to writing a block of code, and see which produces the fastest/most compact IL.

Also, you can't always just go off the IL - sometimes, seemingly slower (and longer) IL can be handled better by the JIT than simpler looking IL. Remember, the IL isn't run directly - it's compiled (and optimized) before running. This is where a sampling profiler can help, or doing your own timings.

However, looking at IL is great for gaining a deeper understanding of how .net works, which is very valuable in the long run.

Reed Copsey
I have ANTS and do perform Sql query tuning. I am gnenerally interested in seeing if the IL can tell me anything about where I can improve my code. As well as that, I generally want to understand how .NET works anyway.
dotnetdev
+1  A: 

LINQPad is used for analyzing the SQL generated from LINQ to SQL operations. Are you looking for database bottlenecks or code performance bottlenecks?

If you are troubleshooting database bottlenecks, use LINQPad to get the raw SQL queries and check them with the SQL Query Analyzer.

If you are looking for code performance bottlenecks, you'll want to use a .NET profiler like ANTS from Red Gate.

Dave Swersky
Hi, I have ANTS and do perform Sql query tuning. I mentioned LINQPAD because I can see the IL very quickly. I am looking for code performance bottlenecks, just thought the IL would give some hints (other than telling me where boxing/unboxing is occuring).
dotnetdev