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:
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:
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:
| 10000 | 100000 | 1000000 | Valid for |
Setup | Hydrate | Total | Setup | Hydrate | Total | Setup | Hydrate | Total | Framework version | C# Version |
Activation and Reflection setter | 0.060 | 0.101 | 0.161 | 0.055 | 0.736 | 0.791 | 0.054 | 6.822 | 6.876 | 1.0, 1.1, 2.0, 3.5, 4.0 | 1.0, 2.0, 3.0, 4.0 |
Activation and Expression Tree setter | 0.300 | 0.003 | 0.303 | 0.313 | 0.049 | 0.359 | 0.293 | 0.578 | 0.871 | 4.0 | none |
Member Initializer | 0.035 | 0.001 | 0.036 | 0.039 | 0.027 | 0.066 | 0.041 | 0.518 | 0.559 | 3.5, 4.0 | 3.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.