RE: Reflection in 2.0 twice as slow? (was Reflection in 1.1 and 2.0)
- From: Atif Aziz <Atif Aziz@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 18 Sep 2006 02:05:02 -0700
Hi Jeffrey,
The problem is in the System.Reflection namespace and nothing to do with
System.ComponentModel or TypeDescriptor. Here's a bit of background. I
originally wrote the sample and pointed out the problem to a distribution
list within our company. Eric kindly posted the problem here with the sample
included verbatim. However, the sample is a little too elaborate to prove the
point. Originally, I was testing the relative performance of doing a
property-set via PropertyDescriptor.SetValue versus PropertyInfo.SetValue. Of
course, PropertyDescriptor.SetValue internally eventually calls
PropertyInfo.SetValue but goes through a lot more hoops (such as engaging
with IComponentChangeService if on e is around) before just doing that and I
wanted to measure the cost of those hoops. My sample code creates its own
variant of PropertyDescriptor implementation that simply calls
PropertyInfo.SetValue. The idea here was to include the cost of v-table
dispatch across both measurements. Anyhow, I've reworked the sample to show
that the performance issue exists solely within the System.Reflection space:
/* ============ C# SAMPLE START ============ */
using System;
using System.Reflection;
public class Person
{
private string _firstName;
private string _lastName;
private int _age;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Runtime version = {0}", Environment.Version);
int iterations = args.Length > 0 ? int.Parse(args[0]) : 1000000;
Console.WriteLine("Iterations = {0}", iterations.ToString("N0"));
Person person = new Person();
PropertyInfo fn = typeof(Person).GetProperty("FirstName");
PropertyInfo ln = typeof(Person).GetProperty("LastName");
PropertyInfo age = typeof(Person).GetProperty("Age");
DateTime start = DateTime.Now;
for (int i = 0; i < iterations; i++)
{
fn.SetValue(person, "John", null);
ln.SetValue(person, "Doe", null);
age.SetValue(person, 42, null);
}
Console.WriteLine(DateTime.Now - start);
}
}
/* ============= C# SAMPLE END ============= */
Assuming the sample is saved as the file Program.cs, compile using:
csc /optimize+ Program.cs
Needless to say, the csc.exe should come from the version of the framework
against which you wish to make performance test. I would recommend compiling
the program with v1.0.3705. Then run it against the various versions using
the following batch to see the differences:
@REM ========== BATCH START ============
@echo off
setlocal
set vers=%1
if "%1"=="" set vers=v1.0.3705 v1.1.4322 v2.0.50727
for %%i in (%vers%) do call :run %%i
goto exit
:run
set COMPLUS_Version=%1
program.exe
:exit
REM ============ BATCH END =============
If you run this against 1.x and 2.0, you'll find that 2.0 is consistently
twice as slow as 1.x for setting a property. On my machine, the results are:
Runtime version = 1.0.3705.6018
Iterations = 1,000,000
00:00:02
Runtime version = 1.1.4322.2032
Iterations = 1,000,000
00:00:02.1093750
Runtime version = 2.0.50727.42
Iterations = 1,000,000
00:00:04.5468750
- Atif
""Jeffrey Tan[MSFT]"" wrote:
Hi Eric,.
Sorry for letting you wait.
I am trying to contact our CLR team regarding this performance issue these
days. After several round of emails, I finally find the correct dev team
that is responsible for System.ComponentModel.TypeDescriptor namespace
changes. The dev team is current working on this issue now.
I will feedback any progress here ASAP. Thanks for your patient.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
- References:
- Reflection in 1.1 and 2.0
- From: Eric
- RE: Reflection in 1.1 and 2.0
- From: "Jeffrey Tan[MSFT]"
- Reflection in 1.1 and 2.0
- Prev by Date: Re: Fusion binding - DEVPATH setting doesn't appear to work
- Next by Date: Assembly will be loaded from ?
- Previous by thread: RE: Reflection in 1.1 and 2.0
- Next by thread: Re: Reflection in 1.1 and 2.0
- Index(es):
Relevant Pages
|
Loading