c++与perl在正则表达式运算速度上的比较

写了两组代码文件,组内代码功能相同:

testv.pl vs testv.cpp

testreg.pl vs testreg.cpp

代码如下:

testreg.cpp/
#include<iostream>
#include<fstream>
#include<regex>
using namespace std;
int main(int argv, char ** argc)
{
    fstream in(argc[1], fstream::in);
    int line_count;
    string line_content;
    regex reg("[ATCG]");
    while(getline(in, line_content))
    {
        line_count++;
        if(line_count % 4 == 2)
        {
            if(regex_search(line_content, reg))
            {
                cout<<1<<endl;
            }
        }
    }
    return 0;
}
testreg.pl/
#!/usr/bin/perl
use strict;
use 5.010;
my $file = shift;
open SEQ, '<', $file or die "$!";
while(<SEQ>) {
        chomp;
        if($. % 4 == 2) {
        if(/[ATCG]/) {
            say 1;
        }
    }
}
testv.cpp/
#include<iostream>
#include<fstream>
#include<unordered_map>
using namespace std;
int main(int argv, char ** argc)
{
    fstream in(argc[1], fstream::in);
    int line_count;
    string line_content;
    typedef unordered_map<string, int> mapdef;
    mapdef mymap;
    while(getline(in, line_content))
    {
        line_count++;
        if(line_count % 4 == 2)
        {
            mymap[line_content]++;
        }
    }
    cout<<mymap.size()<<endl;
    return 0;
}
testv.pl/
#!/usr/bin/perl

use strict;
use 5.010;

my $file = shift;
open SEQ, '<', $file or die "$!";
my %hash;
while(<SEQ>) {
    chomp;
    if($. % 4 == 2) {
        $hash{$_}++;
    }
}
say scalar(keys %hash);

使用shell命令,计算运行时间,结果如下:

time perl testv.pl Input

time ./a.out Input

time perl testreg.pl Input | wc -l

time ./a.out Input | wc -l

 realusersys 
testv.pl0m0.141s0m0.121s0m0.011s 
testv.cpp0m0.077s0m0.054s0m0.012s 
testreg.pl0m0.142s0m0.122s0m0.006s 
testreg.cpp0m0.251s0m0.104s0m0.137s 

其中,Input是fastq文件,含有54914DNA序列。

可以看出在涉及正则表达式运算时, c++明显不占优势,要卡一两面才输出结果

转载于:https://www.cnblogs.com/beita/p/4759923.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值