【发布时间】:2012-03-27 05:47:26
【问题描述】:
我正在处理一个项目的一部分,它从 Postgres 数据库服务器解析日志。 该应用程序是在 C sharp Framework 4.0 中开发的。
生成日志并显示在 DataGridView 上,其中包含以下列
resultCode Statement Starttime Duration
XT001 select * from PizzaMade 01-02-2012 03:10:14 00:04:10
- 有许多格式相同的日志。
- Datagridview 通过解析文本文件从另一个循环中填充。
我的工作是从 Grid 中可用的数据以下列格式生成统计数据
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 ...........
所以基本上统计数据反映了以下内容
- a) 语句已执行
- b) 计算它在网格中出现的次数
- c) 计数百分比,基本上是此语句占据的总计数部分
- d) 包含 starttime,duration 的串联字符串
» 首先使用 for 循环将统计信息生成为 Datatable
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
}
» 填充数据表后,我使用循环来计算 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 的 Compute 方法获取总计数,但我不知道是否会提高性能
-
我在 100 万条记录的样本数据上测试了计算方法,似乎 foreach 循环需要 545 毫秒,计算方法需要 1850 毫秒。即 foreach 循环更快
标签: c# .net winforms datagridview datatable