以下的totalFileCiku为string数组有69558个数据,以head为头的链表有68914个结点。
这段代码为什么会跑得异常的慢呢,大概要19小时。
哪位高手指点一下,问题出在哪里,或者需要怎么优化一下?? /////////////////////////////////////////////////这一块要运行19个钟头!!!!!//////////////////////////////////////
int testHitTime=0;
int testHitTime1=0;
for (current = head.next;current !=null;current = current.next)
{
for(long i = 1; i<=lineNum; i++)
{
//去除空格和TAB符
string[] str2 = System.Text.RegularExpressions.Regex.Split(totalFileCiku[i].Trim(),@"\s+"); Regex r4 = new Regex(@"^[\u4e00-\u9fa5]+$");
//判断汉字
if(str2.Length == 2)
{
if ( r4.IsMatch(str2[0]) ) ci = str2[0];
else if( r4.IsMatch(str2[1])) ci = str2[1];
else
continue;//若不为汉字这该行忽略。
}
else if(str2.Length == 1)
{
if ( r4.IsMatch(str2[0]) ) ci = str2[0];
else
continue;//若不为汉字这该行忽略。
}
else
{
continue;//应该不可能到达这里
}
if(current.word == ci)
{ byte[] btr = System.Text.Encoding.Default.GetBytes(totalFileCiku[i]+"\r\n");
memoryOut.Write( btr,0,btr.Length );//将对应的词库行写入临时内存
testHitTime1++;
} }
testHitTime++;
} /////////////////////////////////////////////////这一块要运行19个钟头!!!!!//////////////////////////////////////
附件是整个程序的源代码。
这段代码为什么会跑得异常的慢呢,大概要19小时。
哪位高手指点一下,问题出在哪里,或者需要怎么优化一下?? /////////////////////////////////////////////////这一块要运行19个钟头!!!!!//////////////////////////////////////
int testHitTime=0;
int testHitTime1=0;
for (current = head.next;current !=null;current = current.next)
{
for(long i = 1; i<=lineNum; i++)
{
//去除空格和TAB符
string[] str2 = System.Text.RegularExpressions.Regex.Split(totalFileCiku[i].Trim(),@"\s+"); Regex r4 = new Regex(@"^[\u4e00-\u9fa5]+$");
//判断汉字
if(str2.Length == 2)
{
if ( r4.IsMatch(str2[0]) ) ci = str2[0];
else if( r4.IsMatch(str2[1])) ci = str2[1];
else
continue;//若不为汉字这该行忽略。
}
else if(str2.Length == 1)
{
if ( r4.IsMatch(str2[0]) ) ci = str2[0];
else
continue;//若不为汉字这该行忽略。
}
else
{
continue;//应该不可能到达这里
}
if(current.word == ci)
{ byte[] btr = System.Text.Encoding.Default.GetBytes(totalFileCiku[i]+"\r\n");
memoryOut.Write( btr,0,btr.Length );//将对应的词库行写入临时内存
testHitTime1++;
} }
testHitTime++;
} /////////////////////////////////////////////////这一块要运行19个钟头!!!!!//////////////////////////////////////
附件是整个程序的源代码。
解决方案 »
- vs2010 winform程序的debug下的exe文件执行后自动退出
- C# 怎么让对话框总在最前边弹出来
- System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
- C#窗体设计,界面布局常见问题
- 大家好谁有关于Socket服务端的程序嘛?C#的。多线程。我收到客户端要根据情况返回一个字符串回去。
- 使用C#时遇到两个问题,请大家帮帮忙。
- 如何控制水晶报表每页只有15行?急呀,在线等..............
- 高分求解----DataList中包含DataGrid的难题
- 用SendARP获取mac时的结果不对!!!!!
- XPath的表达式怎么使用啊?
- DataGridView中下拉框有事件吗?
- 水晶报表中怎样将数据有一行变为一列显示
觉得是这里的问题。
Regex reg = new Regex(@"\s+");
Regex r4 = new Regex(@"^[\u4e00-\u9fa5]+$");
拿到双重循环外面来声明,看看效率能提升多少
Match m;
for (current = head.next; current != null; current = current.next)
{
for (long i = 1; i <= lineNum; i++)
{
m = reg.Match(totalFileCiku[i]);
if (m.Success)
{
if (current.word == m.Value)
{
byte[] btr = System.Text.Encoding.Default.GetBytes(totalFileCiku[i] + "\r\n");
memoryOut.Write(btr, 0, btr.Length);//将对应的词库行写入临时内存
testHitTime1++;
}
}
else
{
continue;//若不为汉字这该行忽略。
}
}
testHitTime++;
}
testHitTime1++;如果是大量的写,你修改为写文件吧。string[] str2 = System.Text.RegularExpressions.Regex.Split(totalFileCiku[i].Trim(),@"\s+");
看看能不能在循环之前就处理掉。
先谢谢大家的帮忙。我要完成的任务就是:用以head为头的链表(有68914个结点)里的值去匹totalFileCiku里string数组(有69558个数据)。
凡是对上了的就将值输出到临时内存中。但是 totalFileCiku里的string数组是用空格分隔的汉字和拼音,
例如: xyz 李玉蓉
关之琳 gzl
要的只是汉字部分。所以要先处理一下。为什么要输出到临时内存而不是文件里呢。因为出现程序崩溃的时候,临时文件不会被删除,再启动的时候就没办法用了或者会产生垃圾文件。所以选择用临时内存。
然后用Dictionary做Hash,如果链表里面的文本内容不是巨大的话,应该19秒之内可以处理完。
for (current = head.next;current !=null;current = current.next)
{
nodes.add(current.word,current.word);
}
int testHitTime=0;
int testHitTime1=0;
Regex r4 = new Regex(@"^[\u4e00-\u9fa5]+$"); for(long i = 1; i<=lineNum; i++)
{
//去除空格和TAB符
string[] str2 = System.Text.RegularExpressions.Regex.Split(totalFileCiku[i].Trim(),@"\s+");
//判断?字
if(str2.Length == 2)
{
if ( r4.IsMatch(str2[0]) ) ci = str2[0];
else if( r4.IsMatch(str2[1])) ci = str2[1];
else
continue;//若不??字??行忽略。
}
else if(str2.Length == 1)
{
if ( r4.IsMatch(str2[0]) ) ci = str2[0];
else
continue;//若不??字??行忽略。
}
else
{
continue;//??不可能到??里
}
string s= nodes.get(ci);
if(s!=null)
{ byte[] btr = System.Text.Encoding.Default.GetBytes(totalFileCiku[i]+"\r\n");
memoryOut.Write( btr,0,btr.Length );//将??的??行写入??内存
testHitTime1++;
} }
/////////////////////////////////////////////////?一?要?行19个??!!!!!//////////////////////////////////////
2 用两个指针遍历两个列表,方法是比较两个指针所指向的值,小的那个指针(假设是升序排列)步进,直到某个指针到达终点。为了方便控制,可以在列表后面追加一个max_value算法如下Int32 i=0,j=0;
Int32 ll=array1.Length-1,mm=array2.Length-1;
while(i<ll&&j<mm){
if(array1[i]==array2[j]){
//发现匹配的内容,处理(比如说加入某个临时内存什么的)
i++;j++;
}else if(array1[i]<array2[j]){
i++;
}else{
j++;
}
}
时间复杂度:
m=68914,n=69558
O(m log m)+O(n log n)+m+n
而链表里的数据都是被筛选出来的,对于关键字也是不能排序的。6楼的方案已经能行了,或者加上48、50楼的办法效率更高。