免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: zhjwlgh01
打印 上一主题 下一主题

函数求优化 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
11 [报告]
发表于 2017-05-22 10:23 |只看该作者
本帖最后由 flw 于 2017-05-22 10:25 编辑

把所有的
  1. if ( cond == true ) {
  2.        blablabla
  3. }
复制代码

都改成
  1. if ( cond != true ) {
  2.     continue / last / return;
  3. }

  4. blablabla;
复制代码


可以大幅度减少嵌套,这是一个很常见的优化点,也是初学者很容易犯的一个错误。

另外,我看你用到了一些循环匹配,可以改成 hash 的 exists 运算,应该会帮到你:
$ perldoc -f exists | head -4
    exists EXPR
            Given an expression that specifies an element of a hash, returns
            true if the specified element in the hash has ever been
            initialized, even if the corresponding value is undefined.

论坛徽章:
2
综合交流区版块每日发帖之星
日期:2016-07-06 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:00
12 [报告]
发表于 2017-05-22 11:15 |只看该作者
下面是完整的函数,目的是取出testCommand 中包含的recordFile,然后再判断是否满足

论坛徽章:
12
子鼠
日期:2014-10-11 16:46:482016科比退役纪念章
日期:2018-03-16 10:24:0515-16赛季CBA联赛之山东
日期:2017-11-10 14:32:142016科比退役纪念章
日期:2017-09-02 15:42:4715-16赛季CBA联赛之佛山
日期:2017-08-28 17:11:5515-16赛季CBA联赛之浙江
日期:2017-08-24 16:55:1715-16赛季CBA联赛之青岛
日期:2017-08-17 19:55:2415-16赛季CBA联赛之天津
日期:2017-06-29 10:34:4315-16赛季CBA联赛之四川
日期:2017-05-16 16:38:55黑曼巴
日期:2016-07-19 15:03:112015亚冠之萨济拖拉机
日期:2015-05-22 11:38:5315-16赛季CBA联赛之北京
日期:2019-08-13 17:30:53
13 [报告]
发表于 2017-05-22 11:26 |只看该作者
本帖最后由 523066680 于 2017-05-22 12:09 编辑

没有测试环境,人肉编辑了一下。不知道能不能正常运行

1. 字符串数组改用qw//括起来省去双引号和逗号。
2. if (xxx ) 改为了 "next unless (xxx);" 少了几层镶嵌,8个空格缩进改为4个空格
3. 左边花括号统一换行
4. foreach 统一改成 for
5. Logger::log(Logger::INFORMATIONAL, "") 这么长,弄到小函数里了

不知道 $testCommand 的内容是咋样的,有些 for + if 可能可以换成 $test ~~ @array 的形式,可能。

my $cmd = undef;
my $cmdresult = undef;
my $recordFile = undef;
my $line = undef;
my $recordFileName = undef;
my $dumpFormat = undef;
my $playList = undef;

# Used for getting the record file
my @recordFileName = qw/cam fromfile dump encoder videosrcsh264enc/;

#Used for getting the dump file which can't be playback in the device
my @dumpFormat = qw/.avi .mkv .h263 .264 .h265 .NV12 .nv12 .dump .mpeg4 .yuv/;

#Used for checking the record file could be playback
my @playList = qw/.avi .mkv .h263  .264  .h265 .mpeg4/;

#core dump format, no need to play
my @nonPlayList = qw/.NV12 .nv12 .dump .yuv/;

my $checkResult = CommonUtils::FAIL;

#Got dunmp file name from the testCommand first;
for $recordFile (split/\/+|\s/,$testCommand)
{
    for $recordFileName (@recordFileName)
    {
        next unless ( $recordFile =~ /$recordFileName/ );
        for $dumpFormat (@dumpFormat)
        {
            next unless ( $recordFile =~ /$dumpFormat/ );
            next unless ( CommonUtils::isExists("$recordPath/$recordFile","-f") );

            loginfo("Found the recorded file: $recordFile");

            for $playList (@playList)
            {
                $checkResult = CommonUtils::FAIL;
                next unless ( $recordFile =~ /$playList/ );

                $cmd = "adb shell su -c 'gst-play-1.0 $recordPath/$recordFile'";
                $cmdresult = Command::getOutput($cmd);

                for $line (split /\n/, $cmdresult)
                {
                    loginfo($line);
                    next unless ($line =~ /Setting pipeline to PLAYING/);

                    loginfo("\n play the recored file: $recordFile successfully\n");
                    $checkResult = CommonUtils::PASS;
                }
            }

            for $playList (@nonPlayList)
            {
                if ($recordFile =~ /$playList/ )
                {
                    $checkResult = CommonUtils::PASS;
                }
            }

            if ($checkResult == CommonUtils::FAIL)
            {
                loginfo("\n Found error with recored file: $recordFile \n");
            }
        }
    }
}

print("checkResult: $checkResult\n\n");
return $checkResult;

sub loginfo
{
    Logger::log(Logger::INFORMATIONAL, shift);
}

论坛徽章:
0
14 [报告]
发表于 2017-06-02 09:15 |只看该作者
回复 13# 523066680

非常感谢~~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP