c++ - Can I increase the performance of this Python code? -



c++ - Can I increase the performance of this Python code? -

i have written next simple loop in python. uses floating point additions , multiplications sum squares of integers 0 1.0e9 - 1. (this toy example, believe representative of code writing).

a = 0.0 = 0.0 while < 1.0e9: += * += 1.0 print

on machine, using 32-bit cpython 2.7.8, takes 400 seconds run. equivalent c++ code (below) runs in less 2 seconds , equivalent go code in less 3.

double = 0.0; for(double = 0.0; < 1.0e9; += 1.0) { += * i; } std::cout << << std::endl;

since code needs distributed end users may not have other cpython installed, unable utilize pypy or numpy speed python code.

is there else can increment performance of python code, or arithmetic heavy work cpython 100 times slower c++ , go?

you have few options:

1) utilize pypy. on programme goes 30x faster:

$ time python sum.py 3.33333332833e+26 real 4m17.728s user 4m17.465s sys 0m0.107s $ time pypy sum.py 3.33333332833e+26 real 0m7.973s user 0m7.614s sys 0m0.049s

2) utilize different algorithm. can utilize algebra same reply much faster. know real code different toy example, there still might opportunities cut down work.

3) utilize numpy, for. if dealing arrays, numpy faster.

4) utilize cython. tried on python code, , ran in .936 seconds without type declarations.

5) utilize c or go. cpython slower, since interpreted, there overhead compute-bound processes.

python c++ performance optimization

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -