Click here to Skip to main content
Click here to Skip to main content
Go to top

Benchmarking Direct, Delegate and Reflection Method Invocations

, 5 Jan 2003
Rate this:
Please Sign up or sign in to vote.
This console mode applet illustrates the significant performance hit of methods invoked using reflection.


This article was inspired by:

Both of these articles have inspired me to do some benchmarking of the three forms of function invocation offered in C#:

  • Direct call
  • via a delegate
  • via reflection

As I am in the prototyping stages of an "application automation layer" (The Application Automation Layer: Introduction And Design [^] and The Application Automation Layer - Design And Implementation of The Bootstrap Loader And The Component Manager [^]) which currently relies heavily on method invocation via reflection, I thought I should take the advice of both of these articles and look at the performance of my current implementation.

The results are rather astonishing.

Benchmarking C#

Didn't I read somewhere on CP that the Microsoft license agreement specifically says "thou shalt not benchmark .NET!"? Well, that's yet another commandment I've broken.

I decided to write a really simple benchmarking program, not for the purposes of gleaning minor differences between the three types of function invocation, but to determine if there are any large differences. The benchmark program compares:

Benchmark Matrix
Type Direct Delegate Reflection
Static, No parameters - - -
Static, With parameters - - -
Instance, No parameters - - -
Instance, With parameters - - -

The results are as follows:

You will notice that reflection is approximately 50 times slower than direct calls. This means that I am going to have to seriously reconsider my implementation in the AAL!

Some Code

The program benchmarks twelve different types of invocation. They all look basically like this:

public static void StaticDirectCallWithoutParams()

simply consisting of a counter increment.

At the beginning of the program, the delegates and reflection methods are initialized along with a couple of embedded constants controlling the number of times the test is run, and how many milliseconds we spend calling the function under test:

CallBenchmark cb=new CallBenchmark();

SNPCall snpCall=new SNPCall(StaticDelegateWithoutParams);
SPCall spCall=new SPCall(StaticDelegateWithParams);
INPCall inpCall=new INPCall(cb.InstanceDelegateWithoutParams);
IPCall ipCall=new IPCall(cb.InstanceDelegateWithParams);

MethodInfo snpMI=GetMethodInfo
MethodInfo spMI=GetMethodInfo
MethodInfo inpMI=GetMethodInfo
MethodInfo ipMI=GetMethodInfo

int sampleSize=20;    // # of samples
int timerInterval=200;  // in ms

A timer is set up to stop the test:

Timer timer=new Timer(timerInterval);
timer.Elapsed+=new ElapsedEventHandler(OnTimerEvent);


static void OnTimerEvent(object src, ElapsedEventArgs e)

And each test looks something like this:

while (!done)
    StaticDirectCallWithParams(1, 2, 3);

Rocket science, isn't it?


This little test clearly shows the performance hit taken by invoking methods using reflection. Even considerations like CPU caching wouldn't account for this significant variance (I think!). Obviously, I need to rethink my architecture implementation.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website,, where you will find many of his articles and his blog.
Marc lives in Philmont, NY.

Comments and Discussions

GeneralThe difference with method body is different (45 % - 50%) Pinmemberasherab15-May-06 10:42 
Generalpoor performance of delegates Pinmemberhnipak5-Aug-03 23:47 
Hello everyone,
I've just come across this usefull article! I tried to run the benchmark on my computer, and I'm surprised with the poor performance of delegates... I did not expect invocation via reflection to be a champ, but delegates are - compared to direct calls - also not showing nice numbers to me, but they are 40x slower that direct calls! (I had to change counter from int to long, because it overflows on my PC).
Static Direct Call Without Params: 44573675
Static Direct Call With Params: 44422734
Instance Direct Call Without Params: 46474033
Instance Direct Call With Params: 47621956
Static Delegate Call Without Params: 1002392
Static Delegate Call With Params: 978512
Instance Delegate Call Without Params: 1689502
Instance Delegate Call With Params: 1609608
Static Reflection Invoke Call Without Params: 45125
Static Reflection Invoke Call With Params: 27282
Instance Reflection Invoke Call Without Params: 43302
Instance Reflection Invoke Call With Params: 26314
GeneralOne question Pinmembermcarbenay14-Jan-03 10:55 
GeneralRe: One question PinmemberMarc Clifton14-Jan-03 12:14 
GeneralMy thoughts again Pinmemberleppie7-Jan-03 7:20 
GeneralRe: My thoughts again PinmemberMarc Clifton7-Jan-03 11:16 
GeneralRe: My thoughts again Pinmemberleppie7-Jan-03 11:54 
GeneralRe: My thoughts again PinmemberJonathan de Halleux22-Sep-03 4:06 
GeneralComment PinmemberJörgen Sigvardsson7-Jan-03 6:40 
GeneralRe: Comment PinmemberMarc Clifton7-Jan-03 11:14 
GeneralRe: Comment PinmemberJörgen Sigvardsson7-Jan-03 12:02 
GeneralRe: Comment PinmemberMarc Clifton7-Jan-03 12:04 
GeneralWith optimized code... PinmemberWesner Moise6-Jan-03 19:16 
GeneralRe: With optimized code... PinmemberMarc Clifton7-Jan-03 12:10 
GeneralRe: With optimized code... PinmemberDavid Stone7-Jan-03 19:11 
GeneralRe: With optimized code... PinmemberMarc Clifton8-Jan-03 0:34 
GeneralRe: With optimized code... PinmemberDavid Stone8-Jan-03 8:16 
GeneralRe: With optimized code... PinmemberMarc Clifton8-Jan-03 10:03 
GeneralRe: With optimized code... PinmemberDavid Stone8-Jan-03 13:50 
GeneralRe: With optimized code... PinmemberSwythan15-Jan-03 0:50 
GeneralRe: With optimized code... PinmemberMarc Clifton15-Jan-03 1:06 
GeneralEval Option PinmemberWesner Moise6-Jan-03 19:06 
GeneralRe: Eval Option PinmemberJörgen Sigvardsson7-Jan-03 6:35 
GeneralSome thoughts... PinmemberMatt Gullett6-Jan-03 17:24 
GeneralRe: Some thoughts... PinmemberMarc Clifton7-Jan-03 1:18 
GeneralRe: Some thoughts... PinmemberMatt Gullett7-Jan-03 2:02 
GeneralRe: Some thoughts... PinmemberMarc Clifton7-Jan-03 11:22 
GeneralRe: Some thoughts... PinmemberMatt Gullett7-Jan-03 13:50 
GeneralRe: Some thoughts... PinmemberDavid Stone7-Jan-03 18:51 
QuestionHow about Reflection Through Delegates Pinmemberbabybg6-Jan-03 17:19 
AnswerRe: How about Reflection Through Delegates PinmemberWesner Moise6-Jan-03 19:04 
GeneralRe: How about Reflection Through Delegates PinmemberMarc Clifton7-Jan-03 12:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 6 Jan 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid