Thursday, December 17, 2020

How fast is C++ compared to Python?

 

How fast is C++ compared to Python?

An example for data scientists who believe they don’t need to know C++



Image for post
Photo by Saffu on Unsplash
Image for post
Photo by author.
Image for post
Photo by author.

A Short Introduction to DNA K-mers

The Challenge

Comparing Solutions

def convert(c):
if (c == 'A'): return 'C'
if (c == 'C'): return 'G'
if (c == 'T'): return 'A'
if (c == 'G'): return 'T' print("Start") opt = "ACGT"
s += opt[0]
s = "" s_last = "" len_str = 13 for i in range(len_str):
counter = 1
for i in range(len_str): s_last += opt[-1] pos = 0 while (s != s_last):
# print(s)
counter += 1 # You can uncomment the next line to see all k-mers.
if (s[i] == opt[-1]):
change_next = True for i in range(len_str): if (change_next):
else:
s = s[:i] + convert(s[i]) + s[i+1:] change_next = True
# print(s)
s = s[:i] + convert(s[i]) + s[i+1:] break # You can uncomment the next line to see all k-mers.
print("Finish!")
print("Number of generated k-mers: {}".format(counter))


#include <iostream>
#include <string>

using namespace std;

char convert(char c)
{
    if (c == 'A') return 'C';
    if (c == 'C') return 'G';
    if (c == 'G') return 'T';
    if (c == 'T') return 'A';
    return ' ';
}

int main()
{
    cout << "Start" << endl; 

    string opt = "ACGT";
    string s = "";
    string s_last = "";
    int len_str = 13;
    bool change_next;

    for (int i=0; i<len_str; i++)
    {
        s += opt[0];
    }

    for (int i=0; i<len_str; i++)
    {
        s_last += opt.back();
    }

    int pos = 0;
    int counter = 1;
    while (s != s_last)
    {   
        counter ++;
        // You can uncomment the next line to see all k-mers.
        // cout << s << endl;  
        change_next = true;
        for (int i=0; i<len_str; 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 to see all k-mers.
    // cout << s << endl;
    cout << "Number of generated k-mers: " << counter << endl;
    cout << "Finish!" << endl; 
    return 0;
}



Image for post
Table 1) Comparing Python and C++ runtimes for generating 13-, 14-, and 15-mers.

The World’s Heaviest Building click here

some abandoned plane incidents: click Here

Oldest technologies scientists still cannot explain: click here


                                  THANKS FOR READING PLEASE SHARE IT 





    c++ ,
    online c compiler ,

    How fast is C++ compared to Python?