Hello,

i am working on a section of a project, that uses large number of sum methods.
These sum methods are applied on a Datatable

To test the best method, i use the following

Datatable structure

    Class ParseLog
    {
    public DataTable PGLStat_Table = new DataTable();
     public LogParser()
     {
         PGLStat_Table.Columns.Add("type", typeof(string)); 
         PGLStat_Table.Columns.Add("desc", typeof(string)); 
         PGLStat_Table.Columns.Add("count", typeof(int));
         PGLStat_Table.Columns.Add("duration", typeof(decimal));
         PGLStat_Table.Columns.Add("cper", typeof(decimal));
         PGLStat_Table.Columns.Add("dper", typeof(decimal));
         PGLStat_Table.Columns.Add("occurancedata", typeof(string));  
     }

}

Following method is used to Fill the table

LogParser pglp = new LogParser();
Random r2 = new Random();
for (int i = 1; i < 1000000; i++)
{
  int c2 = r2.Next(1, 1000);
  pglp.PGLStat_Table.Rows.Add("Type" + i.ToString(), "desc" + i , c2, 0, 0, 0, " ");
}
  • Sum is applied on count column, where value of c2 is updated

Following Methods used to calculate Sum

Method 1 using Compute

Stopwatch s2 = new Stopwatch();
s2.Start();
object sumObject;
sumObject = pglp.PGLStat_Table.Compute("Sum(count)", " ");
s2.Stop();
long d1 = s2.ElapsedMilliseconds;

Method 2 using Foreach loop

s2.Restart();
int totalcount = 0;
foreach (DataRow dr in pglp.PGLStat_Table.Rows)
{
int c = Convert.ToInt32(dr["count"].ToString());
totalcount = totalcount + c;
}
s2.Stop();
long d2 = s2.ElapsedMilliseconds;

Method 3 using Linq

s2.Restart();
var sum = pglp.PGLStat_Table.AsEnumerable().Sum(x => x.Field<int>("count"));
MessageBox.Show(sum.ToString());
s2.Stop();
long d3 = s2.ElapsedMilliseconds;

After Comparison the results are

a) foreach is the fastest 481ms

b) next is linq 1016ms

c) and then Compute 2253ms

Query 1

I accidentally change "c2 to i" in the following statement

pglp.PGLStat_Table.Rows.Add("Type" + i.ToString(), "desc" + i , i, 0, 0, 0, " ");

The Linq statement produces an Error "Arithmetic operation resulted in an overflow."
Whereas the Compute and Foreach loop are still able to complete the computation

Is such a behaviour cause of concern or am i missing a directive ?
(also the figures computed are large)

Query 2

I was under the impression Linq does it fastest, is there a optimized method or parameter
that makes it perform better.

thanks for advice
arvind

I ran the test in debug mode with some slight modifications (making all calculations return a long) and came up with different results:

3508 = Compute
1169 = ForEach
427 = Linq

3412 = Compute
1170 = ForEach
377 = Linq

3388 = Compute
1282 = ForEach
379 = Linq

I used dot net 3.5 on a DELL D620 with iTunes and MS Outlook Loaded.
I also makde this a Console app instead of a WinForms app.

My results show LINQ is more than twice as fast as the the other two methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.