Hello ,

i am working on a section of a project, that parses Logs from Postgres Database Server.
The application is developed in C sharp Framework 4.0.

A log is produced and displayed on a DataGridView with the following columns

resultCode       Statement                  Starttime                   Duration
XT001         select * from PizzaMade    01-02-2012 03:10:14         00:04:10
  • there are many loglines with the sameformat.
  • The Datagridview is filled from another loop by parsing a Textfile.

My work is to generate stats from the data available in Grid in the following format

Statement                                 Count        countpercent                    Occurs_on

select * from PizzaMade                     3             1.42        01/02 at 03pm   [00:04:10], 01/02 at 04 [00:01:04]
select id,qty,table from PizzaMade          12             5.12           ...........

so basically the stats reflect the following

  • a) statement executed
  • b) Count number of times it appears in a grid
  • c) percentage of count which is basically the portion of totalcounts this statement occupies
  • d) a concatenated string containing starttime,duration

» The stats are generated as Datatable first, using a for loop

foreach(DataGridViewRow dr in LogGrid.Rows)
{
// search in the Datatable if the statement is present
// if it is then add count , add starttime and duration to the column
// if not then add a newrow

}

» After populating the datatable , i use a loop to calculate the Totalcount

int totalcount = 0;
foreach (DataRow dtrow in StatTable.Rows)
{
totalcount = totalcount + Convert.ToInt32(dr["count"].ToString());
}

» After calculating the count, there is a loop to calculate the percentage

foreach (DataRow dtrow in StatTable.Rows)
{
    dr["countpercent"] = (count/totalcount)*100;
}

Although everything seems ok, the whole method is sluggish with large number of rows.

  • Can you please suggest methods to improve the performance.

thanks
arvind

Can you do your calculations before the data hits the DataGridView?
If you just wanted raw totals of lines, wouldn't it be easiest to access that data before it is in "display" mode?

No problem if there is a not of rows of code. Sometimes there is no other way. If calculating a sum of some column, you have to do a loop through all the rows to get a value in each row and do the sum.
What exactly would you like to make is "better"? If last loop, there is no need of any better code. Its just fine.

This article has been dead for over six months. Start a new discussion instead.