G
Guest
I've been led to believe by several articles, particularly Eric Gunnerson's
C# Calling Code Dynamically, that calling a method dynamically through
Reflection was much slower than through a Delegate. My testing showed that
actually it was six times faster: 0.5 seconds for 100,000 iterations versus
3.1 seconds.
Can anyone explain why? Something in the way I coded it? I'd appreciate
any insights.
Here's the code (in a Windows Form with two buttons). Operating system is
Windows XP Pro. Visual Studio 2005. .Net Framework 2.0.
const string OP_GREATER_THAN_OR_EQUAL = "op_GreaterThanOrEqual";
const string OP_LESS_THAN_OR_EQUAL = "op_LessThanOrEqual";
const decimal TEST_VAL = 100000.00M;
const decimal BENCHMARK_VAL = 101000.00M;
const int TEST_LIMIT = 100000;
private delegate bool OperatorTest( decimal pTestVal, decimal pBenchmarkVal );
private void btnMethodInfo_Click( object sender, EventArgs e ){
DateTime start = DateTime.Now;
for( int i = 0; i < TEST_LIMIT; i++ )
{
Type t1 = TEST_VAL.GetType();
Type t2 = BENCHMARK_VAL.GetType();
MethodInfo mi = typeof( decimal ).GetMethod(
OP_GREATER_THAN_OR_EQUAL, new Type[] { t1, t2 } );
bool isValid = ( bool )mi.Invoke( null, new object[] { TEST_VAL,
BENCHMARK_VAL } );
}
DateTime stop = DateTime.Now;
Console.WriteLine( "Elapsed time by MethodInfo = " + ( stop- start ) );
}
private void btnDelegate_Click( object sender, EventArgs e ){
DateTime start = DateTime.Now;
for( int i = 0; i < TEST_LIMIT; i++ )
{
Delegate d = Delegate.CreateDelegate( typeof( OperatorTest ), typeof(
decimal ), OP_GREATER_THAN_OR_EQUAL );
bool isValid = ( bool )d.DynamicInvoke( new object[] { TEST_VAL,
BENCHMARK_VAL } );
}
DateTime stop = DateTime.Now;
Console.WriteLine( "Elapsed time by Delegate = " + ( stop - start ) );
}
C# Calling Code Dynamically, that calling a method dynamically through
Reflection was much slower than through a Delegate. My testing showed that
actually it was six times faster: 0.5 seconds for 100,000 iterations versus
3.1 seconds.
Can anyone explain why? Something in the way I coded it? I'd appreciate
any insights.
Here's the code (in a Windows Form with two buttons). Operating system is
Windows XP Pro. Visual Studio 2005. .Net Framework 2.0.
const string OP_GREATER_THAN_OR_EQUAL = "op_GreaterThanOrEqual";
const string OP_LESS_THAN_OR_EQUAL = "op_LessThanOrEqual";
const decimal TEST_VAL = 100000.00M;
const decimal BENCHMARK_VAL = 101000.00M;
const int TEST_LIMIT = 100000;
private delegate bool OperatorTest( decimal pTestVal, decimal pBenchmarkVal );
private void btnMethodInfo_Click( object sender, EventArgs e ){
DateTime start = DateTime.Now;
for( int i = 0; i < TEST_LIMIT; i++ )
{
Type t1 = TEST_VAL.GetType();
Type t2 = BENCHMARK_VAL.GetType();
MethodInfo mi = typeof( decimal ).GetMethod(
OP_GREATER_THAN_OR_EQUAL, new Type[] { t1, t2 } );
bool isValid = ( bool )mi.Invoke( null, new object[] { TEST_VAL,
BENCHMARK_VAL } );
}
DateTime stop = DateTime.Now;
Console.WriteLine( "Elapsed time by MethodInfo = " + ( stop- start ) );
}
private void btnDelegate_Click( object sender, EventArgs e ){
DateTime start = DateTime.Now;
for( int i = 0; i < TEST_LIMIT; i++ )
{
Delegate d = Delegate.CreateDelegate( typeof( OperatorTest ), typeof(
decimal ), OP_GREATER_THAN_OR_EQUAL );
bool isValid = ( bool )d.DynamicInvoke( new object[] { TEST_VAL,
BENCHMARK_VAL } );
}
DateTime stop = DateTime.Now;
Console.WriteLine( "Elapsed time by Delegate = " + ( stop - start ) );
}