免费注册 查看新帖 |

Chinaunix

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

怎样提高这段代码的性能? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-19 09:51 |只看该作者 |倒序浏览
flw 留言:
下回发代码时请用 code 标签将代码括起来。
  1. open (SOURCEPANELPORTTMP,"panel_port.tmp")||die "can not open file:$!.\n";
  2. open (DESTPANEL_PORT,">panel_port.txt")||die "can not open file:$!.\n";
  3. open (SOURCECONFIGROOM,"config_room_resource")||die "can not open file:$!.\n";
  4. open (DESTCONFIGROOMTMP,">config_room_resource.tmp")||die "can not open file:$!.\n";

  5. while( defined (my $panelporttmp = <SOURCEPANELPORTTMP>) ){
  6.         chomp $panelporttmp;
  7.        
  8.         my ($pport_id,$pport_name,$speciality_type,$valid_flg,$pport_def_id,$station_id,$room_id,$panel_id,$panel_row,
  9.                 $panel_col,$pport_rs,$pport_side,$pport_state,$pport_note) = split(/\|/,$panelporttmp,14);
  10.         seek (SOURCECONFIGROOM,0,0);
  11.         while ( defined (my $configroomresource = <SOURCECONFIGROOM>) ){
  12.                 chomp $configroomresource;
  13.                 my ($relation_id,$relation_group_id,$res_id,$res_type,$res_position,$res_state,$res_station_id,$res_room_id,
  14.                         $associate_res_position,$associate_res_id,$associate_res_type,$carry_busi_id,$carry_busi_type,
  15.                         $busi_position,$res_az,$associate_res_az,$bport_logic_name,$res_pport_rs,$left_media_type,$left_media_id,
  16.                         $left_media_position,$right_media_type,$right_media_id,$right_media_position,$carry_busi_seg_id) = split (/\|/,$configroomresource,25);       
  17.                 if ( $associate_res_id == $pport_id ){
  18.                         $associate_res_id = $pportid;
  19.                         $configroomresource = join ('|',$relation_id,$relation_group_id,$res_id,$res_type,$res_position,$res_state,$res_station_id,$res_room_id,
  20.                                 $associate_res_position,$associate_res_id,$associate_res_type,$carry_busi_id,$carry_busi_type,
  21.                                 $busi_position,$res_az,$associate_res_az,$bport_logic_name,$res_pport_rs,$left_media_type,$left_media_id,
  22.                                 $left_media_position,$right_media_type,$right_media_id,$right_media_position,$carry_busi_seg_id);
  23.                         print DESTCONFIGROOMTMP $configroomresource,"\n";                       
  24.                 }
  25.                
  26.                 elsif($res_id == $pport_id){
  27.                         $res_id = $pportid;
  28.                         $configroomresource = join ('|',$relation_id,$relation_group_id,$res_id,$res_type,$res_position,$res_state,$res_station_id,$res_room_id,
  29.                                 $associate_res_position,$associate_res_id,$associate_res_type,$carry_busi_id,$carry_busi_type,
  30.                                 $busi_position,$res_az,$associate_res_az,$bport_logic_name,$res_pport_rs,$left_media_type,$left_media_id,
  31.                                 $left_media_position,$right_media_type,$right_media_id,$right_media_position,$carry_busi_seg_id);
  32.                         print DESTCONFIGROOMTMP $configroomresource,"\n";
  33.                 }
  34.                
  35.         }
  36.         $pport_name = $year . $mon . $day . "/" . $panel_row . "/" . $panel_col ."端子" ;
  37.         $pport_id = $pportid;
  38.         $panelporttmp = join('|',$pport_id,$pport_name,$speciality_type,$valid_flg,$pport_def_id,$station_id,$room_id,$panel_id,$panel_row,
  39.                 $panel_col,$pport_rs,$pport_side,$pport_state,$pport_note);
  40.         print DESTPANEL_PORT $panelporttmp,"\n";
  41.         $pportid++;
  42. }

  43. close (SOURCEPANELPORTTMP)||die "can not close file:$!.\n";
  44. close (DESTPANEL_PORT)||die "can not close file:$!.\n";
  45. close (SOURCECONFIGROOM)||die "can not close file:$!.\n";
  46. close (DESTCONFIGROOMTMP)||die "can not close file:$!.\n";
复制代码


其中,config_room_resource文件有将近两百万行.

[ 本帖最后由 flw 于 2006-5-19 09:55 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-05-19 10:26 |只看该作者
不知道简化成这样你满意吗?
  1. open (FH,"config_room_resource")||die "can not open file:$!.\n";
  2. my (%res_id, %associate_res_id);
  3. while ( <FH> ){
  4.     chomp;
  5.     my @fields = split (/\|/,$_,25);
  6.     @fields[($#fields+1)..24] = map {''} (1..25);
  7.     $res_id{$fields[2]} = \@fields;
  8.     $associate_res_id{$fields[9]} = \@fields;
  9. }
  10. close (FH)||die "can not close file:$!.\n";

  11. open (DESTCONFIGROOMTMP,">config_room_resource.tmp")||die "can not open file:$!.\n";
  12. open (DESTPANEL_PORT,">panel_port.txt")||die "can not open file:$!.\n";

  13. open (FH,"panel_port.tmp")||die "can not open file:$!.\n";
  14. while( <FH> ){
  15.     chomp $_;
  16.     my @fields = split(/\|/,$_,14);
  17.     @fields[($#fields+1)..13] = map {''} (1..14);
  18.     my ($pport_id, $pport_name, $panel_row, $panel_col) = @fields[0,1,8,9];
  19.     if ( exists $associate_res_id{$pport_id} ){
  20.         $associate_res_id{$pport_id}->[9] = $pportid;
  21.         print DESTCONFIGROOMTMP join ('|', @{$associate_res_id{$pport_id}}), "\n";
  22.     }
  23.     elsif( exists $res_id{$pport_id} ){
  24.         $res_id{$pport_id} = $pportid;
  25.         print DESTCONFIGROOMTMP join ('|', @{$associate_res_id{$pport_id}}), "\n";
  26.     }
  27.            
  28.     $pport_name = "$year$mon$day/$panel_row/$panel_col端子";
  29.     $pport_id = $pportid;
  30.     @fields[0,1,8,9] = ($pport_id, $pport_name, $panel_row, $panel_col);
  31.     print DESTPANEL_PORT join('|',@fields), "\n";
  32.     $pportid++;
  33. }

  34. close(FH);

  35. close (DESTPANEL_PORT)||die "can not close file:$!.\n";
  36. close (DESTCONFIGROOMTMP)||die "can not close file:$!.\n";
复制代码


BTW:我不知道你的程序是干什么的,
所以我所做的每一步,只是进行了等效语法的转换。

[ 本帖最后由 莫愁 于 2006-5-19 10:52 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2006-05-19 10:30 |只看该作者
原帖由 nv2test 于 2006-5-19 09:51 发表

其中,config_room_resource文件有将近两百万行.

如果有两百万行的话,那么至少应该也有 500M 吧?那么我刚才那个程序占的内存就太多啦。
不知道 panel_port.tmp 有多大呢?如果它小的话,就可以用同样的思路,把 panel_port 装入内存。
然后逐行处理 config_room_resource 文件。

[ 本帖最后由 莫愁 于 2006-5-19 10:32 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-05-19 10:32 |只看该作者
你原来那个程序慢就慢在双重循环上,其结果是个笛卡儿积,重复的 IO 操作浪费了很多时间。
只要去掉双重循环,速度应该能快一个数量级。

论坛徽章:
0
5 [报告]
发表于 2006-05-23 13:31 |只看该作者

多谢,莫愁,

我刚开始学习perl .
贴子里的代码是我写的一个工具的一部分.工具的目的,就是替换文件里的几个字段值,生成新的文件.
你给我的回复里,我还在研究.因为有些代码我看不很懂.还在慢慢尝试.

多谢你了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP