Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Hydrating Objects With Expression Trees - Part III

4.00/5 (1 vote)
17 Aug 2010CPOL 13.5K  
Performance comparisons between the different methods of hydrating objects

free hit counters

To finalize this series on object hydration, I'll show some performance comparisons between the different methods of hydrating objects.

For the purpose of this exercise, I'll use this class:

C#
class SomeType
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTimeOffset CreationTime { get; set; }
    public Guid UniqueId { get; set; }
}

and this set of data:

C#
var data = (
    from i in Enumerable.Range(1, ObjectCount)
    select new object[] { i, i.ToString(), DateTimeOffset.Now, Guid.NewGuid() }
).ToArray();

The data below shows the time (in seconds) for different runs for different values of ObjectCount (in the same machine with approximately the same load) as well as its availability for different version of the .NET Framework and the C# programming language:

 100001000001000000Valid for
SetupHydrateTotalSetupHydrateTotalSetupHydrateTotalFramework versionC# Version
Activation and Reflection setter0.0600.1010.1610.0550.7360.7910.0546.8226.8761.0, 1.1, 2.0, 3.5, 4.01.0, 2.0, 3.0, 4.0
Activation and Expression Tree setter0.3000.0030.3030.3130.0490.3590.2930.5780.8714.0none
Member Initializer0.0350.0010.0360.0390.0270.0660.0410.5180.5593.5, 4.03.0, 4.0

These values will vary with the number of the objects being hydrated and the number of its properties, but the method using the Member Initializer will be the most performant.

Code samples for this series of posts (and the one about object dumping with expression trees) can be found on my MSDN Code Gallery: Dump And Hydrate Objects With Expression Trees.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)