Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between the speed of Python and C++

Shulou Source: shulou.com Published: 2022-06-03 03:18:58 10月04日 Update

This article mainly explains the "what is the difference between the speed of Python and C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between Python and C++?"

Introduction to DNA K-mers

DNA is a long-chain unit called nucleotides. There are four types of nucleotides in DNA, which are represented by the letters A, C, G and T respectively. Humans (or, more accurately, Homo sapiens) have 3 billion nucleotide pairs. For example, a small portion of human DNA might be similar to:

ACTAGGGATCATGAAGATAATGTTGGTGTTTGTATGGTTTTCAGACAATT

In this example, if any four consecutive nucleotides (that is, letters) are selected from the string, it will be a k-mer of length 4 (called 4-mer). Here are some 4-mers examples derived from this example: ACTA,CTAG,TAGG,AGGG,GGGA, etc.

Difficult and challenging

This article takes the generation of all possible 13-mers as an example, which is mathematically a permutation problem with substitution. Therefore, there are 4 (67108864) possible 13-mers. Here is a simple algorithm to generate the results in C++ and Python.

Scheme comparison

To make it easier to compare C++ and Python in this particular challenge, I used exactly the same algorithm in both languages. Both types of code are deliberately simple and similar in design. At the same time, avoid complex data structures or third-party packages or libraries. The first piece of code is written in Python.

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]): ss = s [: I] + convert (s [I]) + s [iTun1:] Change_next = True else: ss = s [: I] + convert (s [I]) + s [item1:] break # You canuncomment the next line to see all k-mers. # print (s) print ("Number ofgenerated k-mers: {}" .format (counter)) print ("Finish!")

It takes about 61.23 seconds to run the Python code and generate 67 million total 13-mers. To make a fair comparison, I commented out the line showing k-mers. You can also uncomment these two lines if you want to display them when you generate the k-mers. Note that it takes a long time to display all k-mers. If necessary, operate the CTRL+C abort code.

Now, take a look at the same algorithm in C++:

# include # include usingnamespacestd; charconvert (charc) {if (c ='A') return'C'; if (c = ='C') return'G'; if (c = ='G') return'T'; if (c = ='T') return'A'; return'' } intmain () {cout

Tags: Examples runs code generation speed data nucleotides algorithms results problems learning saliency same human memory content letters differences comments Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi Shulou Information Shulou Tech Info NVidia