Slower than Reflection: Take Two

This is a hugely belated reply to:

I’m looking to perform some instrumentation on some already compiled code, and part of that involves getting a handle to a previous method. I wondered how slow accessing this through the StackTrace would actually be, and it turns out to be too slow according to that article.

I was hoping that Kozmic’s approach to getting the entire stack trace before filtering was causing the massive performance hit, so I did some benchmarks of my own…

Profile(() => new System.Diagnostics.StackTrace(false).GetFrame(1).GetMethod(), "Unfiltered StackFrame");

Profile(() => new StackFrame(1, false).GetMethod(), "Filtered StackFrame");


The unfiltered version is comparable to Kozmic’s example, and the filtered example is supposed to be more performant as we don’t need the entire stack trace. But the question is, “Is it faster?”. Let’s see:


(Results in milliseconds)

Yes it is!  However, quite unfortunately it is still not performant enough for my purposes. If you want to try out your own benchmarks the code is:

blog comments powered by Disqus