Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 285

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report