# 与Python的速度较量：C++究竟有多快？

Python和C++类语言之间存在许多区别，本文将通过一个十分简单的例子向你展示，与Python相比，C++究竟有多快。

DNA K-mers简介

DNA是一种称为核苷酸的长链单位。在DNA中，共有4种核苷酸类型，分别用字母A、C、G和T表示。人类(更准确地说是智人)拥有核苷酸对30亿个。例如，人类DNA的一小部分可能类似于：

ACTAGGGATCATGAAGATAATGTTGGTGTTTGTATGGTTTTCAGACAATT

defconvert(c):

if (c ==’A’): return’C’

if (c ==’C’): return’G’

if (c ==’G’): return’T’

if (c ==’T’): return’A’

print(“Start”)

opt =”ACGT”

s =””

s_last =””

len_str =13

for i inrange(len_str):

s += opt[0]

for i inrange(len_str):

s_last += opt[-1]

pos =0

counter =1

while (s != s_last):

counter +=1

# You can uncomment the next line to see all k-mers.

# print(s)

change_next =True

for i inrange(len_str):

if (change_next):

if (s[i] == opt[-1]):

s = s[:i] +convert(s[i]) + s[i+1:]

change_next =True

else:

s = s[:i] +convert(s[i]) + s[i+1:]

break

# You canuncomment the next line to see all k-mers.

# print(s)

print(“Number ofgenerated k-mers: {}”.format(counter))

print(“Finish!”)

#include

#include

usingnamespacestd;

charconvert(char c)

{

if (c == ‘A’) return’C’;

if (c == ‘C’) return’G’;

if (c == ‘G’) return’T’;

if (c == ‘T’) return’A’;

return’ ‘;

}

intmain()

{

cout << "Start" << endl;

string opt = “ACGT”;

string s = “”;

string s_last = “”;

int len_str = 13;

bool change_next;

for (int i=0; i

{

s += opt[0];

}

for (int i=0; i

{

s_last += opt.back();

}

int pos = 0;

int counter = 1;

while (s != s_last)

{

counter ++;

// You canuncomment the next line to see all k-mers.

// cout << s<< endl;

change_next = true;

for (int i=0; i

{

if (change_next)

{

if (s[i] == opt.back())

{

s[i] = convert(s[i]);

change_next = true;

} else {

s[i] = convert(s[i]);

break;

}

}

}

}

// You can uncomment the next line tosee all k-mers.

// cout << s << endl;

cout << "Number of generated k-mers: " <

cout << "Finish!" << endl;

return0;

}

【责任编辑：赵宁宁 TEL：（010）68476606】