您的位置:首页 > 编程语言 > C语言/C++

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

2015-08-26 11:46 555 查看
写了两组代码文件,组内代码功能相同:

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++明显不占优势,要卡一两面才输出结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: