I while ago I’ve run across this post by Ayende regarding performance implications when creating objects. I’ve decided to play with his sample code in order to learn some of his techniques regarding IL generation and DynamicMethod. I’ve also found this post by Roger Alsing, which adds another variation to Ayende’s original post. Please, read both posts before reading the rest of mine.
When I was messing with that, I’ve adjusted the samples just to get the benchmark results more easily, so that it displays like this:
I’ve also refactored the sample code just to make it easier for me to try out different approaches (like Roger’s approach using Expression Trees), plug in the benchmark logger, etc.
If you’re interested, you can download a solution containing the full sample code here, so you can try it out yourself.