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
其中,Input是fastq文件,含有54914DNA序列。
可以看出在涉及正则表达式运算时, c++明显不占优势,要卡一两面才输出结果
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
real | user | sys | ||
testv.pl | 0m0.141s | 0m0.121s | 0m0.011s | |
testv.cpp | 0m0.077s | 0m0.054s | 0m0.012s | |
testreg.pl | 0m0.142s | 0m0.122s | 0m0.006s | |
testreg.cpp | 0m0.251s | 0m0.104s | 0m0.137s |
可以看出在涉及正则表达式运算时, c++明显不占优势,要卡一两面才输出结果
相关文章推荐
- 【资料整理】C语言位运算总结
- C++多态
- WTL小问题解决方法汇总
- LeetCode 215. Kth Largest Element in an Array
- [VS2010]_[初级]_[VC++ Express 使用WDK的ATL编译出现的问题解决办法]
- [VS2010]_[初级]_[VC++ Express 使用WDK的ATL编译出现的问题解决办法]
- C语言中字符串的内存地址操作的相关函数简介
- C++中的标准库类型
- C# 中动态调用C++动态链接
- C# 中动态调用C++动态链接
- 黑马程序员——C语言中的数据
- C++ 预处理、编译、汇编、链接
- 对比C语言中memccpy()函数和memcpy()函数的用法
- 《算法导论》的桶排序C++实现
- C++ STL 中的 bitset 用法
- [DP]HDOJ1158 Employment Planning
- C语言判断大小端对齐
- C语言strtok()函数:字符串分割
- C++注释规范
- C++虚继承的内存模型