|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Contents
IntroductionMost of you, like myself, have been watching the release of VS 2008 from its beta versions. I have already explored some features of Silverlight; as seen here. From the perspective of this article Microsoft added generics, query operators and LINQ support. I always wondered why LINQ was added when the world was comfortable with ADO programming with its support extended to objects and XML too (and similarly, the use of generics/aggregations etc. in C # 3.0). This question created the purpose of this article. This article seeks to compare existing approaches with the new ones released with C# 3.0 and LINQ. As the scope of comparison is quite large, I will restrict our discussion to:
Before we jump into this article, I want to point out that there are many ways to accomplish a task programmatically. An effort has been made to write the best code possible, though, there are extra lines introduced so to make the comparison for writing LINQ and ADO functions more fair. As the code is shareware you are free to improve and build on it. The basic unit of measurement is counting the ticks which have been used in this article ( Logic for running the function 500 times is given below
The configuration used for getting the current performance data is given below
BackgroundWhat you Need to Run the Samples
Before you Begin Read the FollowingIf you are not familiar with LINQ, new features of C# 3.0 please read the following URLs
Using the CodeInserting a row — ADO vs. LINQ Using a Stored ProcedureFunction used for ADO is Inserting a Row ADO vs LINQ without Using a Stored ProcedureFunction used for ADO is ADO vs. LINQ Reading from a TableFunction used for ADO is Reading XML Files ADO vs. LINQFunction used for ADO is Accessing Objects (Arrays in this case) [Traditional vs. c#3.0 vs. LINQ]Function used for LINQ part is All the functions first create an array of integers, and then crate a second array of only even numbers with their squares. The last step in the functions is to add all the values in the resultant subset. Filling Dataset using LINQ and ADO and then Performing Filter OperationsFunction used for LINQ part is Points of InterestI have referenced the median value rather then the mean to help reduce the effect of outliers in the graph because in a Windows OS there are always more processes running and spikes in the graph do not necessarily mean a fault in the code. Inserting a Row — ADO vs. LINQ Using a Stored Procedure
![]() Inserting a Row — ADO vs. LINQ without Using a Stored Procedure
![]() Reading from a Table (ADO vs. LINQ)
![]() Reading XML Files ADO vs. LINQ
![]() Accessing Objects (Arrays in this Case) [Traditional vs. c#3.0 vs. LINQ]
double sum = nums.Aggregate(delegate(double Cursum, double curNum)
{
if (curNum % 2 == 0)
{
return (Cursum + (curNum * curNum));
} else
{
return (Cursum + 0);
}
});
LINQObj1 which runs a LINQ styled query on the array as given below. Please note that instead of the newFunction(temp) we could also have written temp%2 == 0, I just wanted to demonstrate using functions in the condition clause of the LINQ query. var getSquaresLessthen500 = from temp in nums where temp == newFunction(temp) select temp*temp;
csharpObjects1().![]() Filling Dataset using LINQ and ADO and then Performing Filter OperationsAnd there is a big difference between mean values. I think that in the LINQ implementation the line where we create an object of DataRow and then add to the table is the place where performance is hitting. ADO implementation wins here. table.LoadDataRow(new Object[] {
tempRec.CustomerID, tempRec.TerritoryID, tempRec.AccountNumber, tempRec.CustomerType,
tempRec.rowguid, tempRec.ModifiedDate}, true);
So my conclusion is that LINQ is not the overall winner (as expected). While insert operations are better in LINQ, the read operation is superior in ADO. XML operations are by and large the same (not too much of a difference) and object access is basically depending on the type used (even still, aggregate was a good example). LINQ to datasets is pretty costly; I suggest using the ADO version and only use LINQ to objects if we already have dataset available and want to query. For further improvement we should try and do bulky insert operations and also reads of different kind (like integer read, string read, block data read and substring reads). Again it is hard to draw conclusions based on the limited number of scenarios I have covered. There is a scope of improvement in each scenario, but since I have timed all the operations, this will give us better insights when we design or architect next time. AppendixMean and Median Values
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||