Sunday, July 18, 2021

UPDATE as of 18/07/201 for C++ versus Python solving one problem from the field of number theory

 Consider following code implementing the same business logic as procVector.cpp

(.env) [boris@fedora33server NEWFORWARD]$ cat procVector2.cpp

#include <iostream>

#include <vector>

#include <cstddef>

#include <cmath>

#pragma GCC diagnostic ignored "-Wsign-compare"

namespace abc {

std::vector<std::vector<int> >  resultList(int m, int n)

{

   std::vector<std::vector<int> > vec;

   std::vector<int> v1;

   int j,cCount,cntDiv;


    for (int i = m; i <= n  ; i++) {

       cCount = 0 ;

       cntDiv = 0 ; 

       j = 2 ;

      while (cCount <= 1 && j <= int(sqrt(i)))

      {

          if (i%j == 0)

          {

           cntDiv = j;

           cCount +=1;

          }

          j += 1;

      }           

      if (cCount == 1 && cntDiv != i/cntDiv) 

      {        

         v1.push_back(cntDiv);

         v1.push_back(i/cntDiv);

         vec.push_back(v1);

         while (!v1.empty())

         {

             v1.pop_back();

         }

       }

     } 

    return vec;

};

} //namespace

Now update setup.py as follows and reinstall

from distutils.core import setup, Extension

import sysconfig

language = 'c++'

std = 'c++20'

default_compile_args = sysconfig.get_config_var('CFLAGS').split()

extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]

setup(name = 'myModule', version = '1.0',  \

   ext_modules = [Extension('myModule', ['mainVector.cpp','procVector2.cpp'])])

Now update MyProg.py

(.env) [boris@fedora33server NEWFORWARD]$ cat MyProg.py

import myModule

# a = int(input("Input start   number : "))

# b = int(input("Input finish  number : "))

# a = 174457

# b = 174505

a = 1744457

b = 3044505

myModule.resList(a,b)

lines = []

with open('./outvector.dat') as f:

    lines = f.readlines()

Run with new version 

for line in lines:

    print(line)

Now run with new version of procVector.cpp

(.env) [boris@fedora33server NEWFORWARD]$ cat ./speed.sh
date
python MyProg.py
date
(.env) [boris@fedora33server NEWFORWARD]$ ./speed.sh | tee log

(.env) [boris@fedora33server NEWFORWARD]$ head -5 log

Sun Jul 18 06:49:48 PM MSK 2021
643 2713 

2 872231 

(.env) [boris@fedora33server NEWFORWARD]$ tail -5 log
3 1014833 

199 15299 

Sun Jul 18 06:49:52 PM MSK 2021


No comments:

Post a Comment