免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1538 | 回复: 2
打印 上一主题 下一主题

[急]如何提高文件匹配效率,效率太低了。。。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-04-11 15:29 |只看该作者 |倒序浏览
本帖最后由 zhlong8 于 2013-04-11 23:40 编辑

存在一个需要比对的文件,里面有若干个字段,需要在各个目录下的一批文件中找到与之匹配的文件,我用了一下方法:
...
...
...
chdir "$DIR_PP1_ELSE" or die "cannot chdir to $DIR_PP1_ELSE:$!";
my @pp1_else=();
opendir(DIR_TMP,$DIR_PP1_ELSE) or die "error in opening dir $DIR_PP1_ELSE\n";
@pp1_else=grep(/^\d\d\d\d\.0314.*/,readdir DIR_TMP);
closedir DIR_TMP;
...
...
open(FILE,$DF_LIST) or die "can not open file111,please check.($!)";
while(<FILE>)
{
#对方独有话单按条入数组,以待匹配
my @list_df=split /;/, $_;
chdir "$DIR_PP1_ELSE" or die "cannot chdir to $DIR_PP1_ELSE:$!";
my @pp1_else_tmp=@pp1_else;
foreach (@pp1_else_tmp){  #遍历文件
my $filename=$_;
open(MYFILE,$_) or die "can not open file222,please check.($!)";
while(<MYFILE>)           #某个文件遍历记录
{
        my @list_pp1_else=split /,/, $_;
            if($list_pp1_else[9]=~$list_df[0] && $list_pp1_else[11]=~$list_df[1])
           {
            do something;        
        }
}
close(MYFILE);
}   #pp1 else匹配结束

}  #end while 对方独有话单轮询匹配结束
close(FILE);


发现效率特别底,文件大概在1.5w个左右。
那位大虾做个类似的开发,能指点一下吗,谢谢。

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2013-04-12 13:41 |只看该作者
换个思路吧,每一条都要扫描所有文件效率怎么可能高。

论坛徽章:
0
3 [报告]
发表于 2013-04-12 14:37 |只看该作者
如果你真的要這麼做

那一次讀檔可能會快一點

比方說
  1. chdir "$DIR_PP1_ELSE" or die "cannot chdir to $DIR_PP1_ELSE:$!";
  2. my @pp1_else=();
  3. opendir(DIR_TMP,$DIR_PP1_ELSE) or die "error in opening dir $DIR_PP1_ELSE\n";
  4. @pp1_else=grep(/^\d\d\d\d\.0314.*/,readdir DIR_TMP);
  5. closedir DIR_TMP;
  6. ...
  7. ...
  8. open(FILE,$DF_LIST) or die "can not open file111,please check.($!)";
  9. while(<FILE>)
  10. {
  11.         my @list_df=split /;/, $_;
  12.         chdir "$DIR_PP1_ELSE" or die "cannot chdir to $DIR_PP1_ELSE:$!";
  13.         my @pp1_else_tmp=@pp1_else;
  14.        
  15.         foreach(@pp1_else_tmp)
  16.         {
  17.                 local $/;
  18.                 my $filename=$_;
  19.                 open(MYFILE,$_) or die "can not open file222,please check.($!)";
  20.                 $data=<MYFILE>;
  21.                 while($data=~/$list_df[0],.*?,$list_df[1]/g)
  22.                 {
  23.                         do something;
  24.                 }
  25.                 close(MYFILE);
  26.         }
  27. }
  28. close(FILE);
复制代码
不過為什麼不先將所有的比對資料讀進來再做比對呢?

比方說
  1. ...
  2. ...
  3. ...
  4. chdir "$DIR_PP1_ELSE" or die "cannot chdir to $DIR_PP1_ELSE:$!";
  5. my @pp1_else=();
  6. opendir(DIR_TMP,$DIR_PP1_ELSE) or die "error in opening dir $DIR_PP1_ELSE\n";
  7. @pp1_else=grep(/^\d\d\d\d\.0314.*/,readdir DIR_TMP);
  8. closedir DIR_TMP;
  9. foreach (@pp1_else_tmp)
  10. {
  11.         my $filename=$_;
  12.         open(MYFILE,$_) or die "can not open file222,please check.($!)";
  13.         while(<MYFILE>)
  14.         {
  15.                 my @list_pp1_else=split /,/, $_;
  16.                 push(@{$compare_file{$list_pp1_else[9].":".$list_pp1_else[11]}},$filename);
  17.         }
  18.         close(MYFILE);
  19. }
  20. ...
  21. ...
  22. open(FILE,$DF_LIST) or die "can not open file111,please check.($!)";
  23. while(<FILE>)
  24. {
  25.         my @list_df=split /;/, $_;
  26.         if($compare_file{$list_df[0].":".$list_df[1]})
  27.         {
  28.                 chdir "$DIR_PP1_ELSE" or die "cannot chdir to $DIR_PP1_ELSE:$!";
  29.                 foreach(@{$compare_file{$list_df[0].":".$list_df[1]}})
  30.                 {
  31.                         do something;
  32.                 }
  33.         }
  34. }
  35. close(FILE);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP