views:

75

answers:

1

I was looking for an object formatter and templater.

http://haacked.com/archive/2009/01/14/named-formats-redux.aspx

I looked into HenriFormatter and when check performance found that for the same object Type first invocation - causes 15x more time than for next - 15k ticks, second was around 1k. I become digg, and found that its using DataBinder.Eval, that uses reflection, and on some level looks like Type structure is caching.

In compare with the same String.Format that was around 50-100 ticks.

So I a wondering, what if instead of using DataBinder.Eval, we can Emit String.Format code and cache it, and get 8 times performance. But before do all this, I was interested if something was already done anywhere.

+1  A: 

well, you can analyze format string and compose result string.Format call using Expression trees. Just for the test I've made POC implementation it based on Scott Hanselmann's parsing routine (removed custom format capabilities for the simplicity): source code. On my maching results comparing to HenryFormatter were the following:

Instance of Student type : Name=John, [email protected] and birthdate=3/20/1 983 12:00:00 AM Instance of Student type : Name=John, [email protected] and birthdate=3/20/1 983 12:00:00 AM HenriFormat: Average run time for 500000 runs = 00:00:00.0000045 StructureToString: Average run time for 500000 runs = 00:00:00.0000003

desco