我正在處理項目的一部分,它解析Postgres數據庫服務器中的日誌。 該應用程序是在C sharp Framework 4.0中開發的。從Datagridview行生成統計信息
日誌產生和上一個DataGridView顯示有以下的列
resultCode Statement Starttime Duration
XT001 select * from PizzaMade 01-02-2012 03:10:14 00:04:10
- 有許多loglines與sameformat。
- 通過解析文本文件從另一個循環填充Datagridview。
我的工作是生成數據可用的統計信息網格的格式如下
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 ...........
所以基本的統計數據反映了以下
- 一)語句執行
- b )計數出現在網格中的次數
- c)計數的百分比,其基本上是totalcounts此語句佔據
- d)含有開始時間級聯串,持續時間
»該統計信息是作爲數據表最初產生,使用for循環
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
}
»填充DataTable之後,我用一個循環來計算Totalcount
int totalcount = 0;
foreach (DataRow drin StatTable.Rows)
{
totalcount = totalcount + Convert.ToInt32(dr["count"].ToString());
}
»計算計後,有一個循環計算百分比
foreach (DataRow dr in StatTable.Rows)
{
int c = Convert.ToInt32(dr["count"].ToString());
dr["countpercent"] = (c/totalcount)*100;
}
雖然一切看起來正常,整個方法是緩慢的具有大量的行。
- 請問您能否提出改進性能的方法。
感謝 阿文德
你可以嘗試爲獲得總數使用DataTable的計算方法,但我不知道是否會提高性能 – mslliviu 2012-03-27 05:58:38
我測試的計算方法上的100萬所記錄的樣本數據,似乎foreach循環需要545ms並計算方法1850毫秒。即foreach循環更快 – arvind 2012-03-27 06:26:10