NAVmoble - the pocket-sized ERP
Optimized for Microsoft Dynamics NAV and Windows Mobile powered devices

Sunday, November 20, 2005

.NET Compact Framework 2.0 Performance and Reflection

   If you spent your time researching performance improvement tips, soon you will find that reflection is a great but expensive feature in terms of performance.
Let's see some metrics, then!
   My test environment is just the same as in my previous post
We've got HTC QTEC 9090, VS2005,CF2.0,Sql Ce 3.0.
I used the same database structure and 10000 rows to fetch. Every scenario makes 5 subsequent runs. Application is restarted before each test scenario run. Scenario 1 is not listed here , because it uses DataRow instances to represent database table rows.
The code is almost the same. I changed only the way the BObject instance is created. It is created by using Activator.CreateInstance method, this time.

List<BObject> boList = new List<BObject>();
...
IDataReader reader = cmd.ExecuteReader();
...
while(reader.Read())
{
//This was the old instance creation
//BObject bo = new BObject();


//This is the new extra time-consuming instance creation
BObject bo = (BObject)Activator.CreateInstance(typeof(BObject));
bo.id = reader.GetInt32(0);
bo.data1 = reader.GetString(1);
bo.data2 = reader.GetDateTime(2);
bo.data3 = reader.GetDouble(3);
boList.Add(bo);
}


I repeated the same testing scenarios and here come results:
Scenario 2: SqlCeDataReader
#Time[ms]Memory[bytes]Time increase[%]
11006496221699
283441125212142
382611295408136
482881422712143
58333952280129

Scenario 3-a:SqlCeResultSet,None
#Time[ms]Memory[bytes]Time increase[%]
11057696206497
290911125200133
390291295396131
490621422700129
59019952128133

Scenario 3-b:SqlCeResultSet,Insensitive
#Time[ms]Memory[bytes]Time increase[%]
115486996206427
214016112520029
313886129539628
414118142270029
51410595212830

Scenario 3-c:SqlCeResultSet,Scrollable,Updatable,Sensitive
#Time[ms]Memory[bytes]Time increase[%]
11405496206442
212494112520070
312369129539664
412471142270065
51240695212867


   We've got ~25-140 percents time increasing, due to using this resource hungry feature! There is also a small memory overhead. It is not as meaningful as the time increase. However the memory increase was stable across all the test scenario runs, that I've performed. It is interesting that testing scenarios 2 and 3-a(the faster data access methods) report far greater time increasing compared with the rest ones. The time diff. between scenario 3-a and 3-b in the previous test cases had a mean value of ~60%. In this case the diff. is only ~30% due to performance loss in scenario 3-a.
   One of the reasons, which motivated me to perform these tests was that Activator.CreateInstance pattern is very useful, when extendability and flexibility are required.
And while this pattern is frequently used in the desktop environment, it comes with a great cost in the CF world.

No comments: