Comparison between C + + and Perl in the computation speed of regular expressions

Source: Internet
Author: User

Two sets of code files are written, and the code functions in the group are the same:

testv.pl vs Testv.cpp

testreg.pl vs Testreg.cpp

The code is as follows:

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);

Using the shell command, calculate the run time with the following results:

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

Where input is a FASTQ file that contains a 54914DNA sequence.

It can be seen that in the case of regular expression operations, C + + obviously does not dominate, to the card one or two face output results

Comparison between C + + and Perl in the computation speed of regular expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.