Improving performance of Python code using rewriting rules technique

K.A. Zhereb

Abstract


Python is a popular programming language used in many areas, but its performance is significantly lower than many compiled languages. We propose an approach to increasing performance of Python code by transforming fragments of code to more efficient languages such as Cython and C++. We use high-level algebraic models and rewriting rules technique for semi-automated code transformation. Performance-critical fragments of code are transformed into a low-level syntax model using Python parser. Then this low-level model is further transformed into a high-level algebraic model that is language-independent and easier to work with. The transformation is automated using rewriting rules implemented in Termware system. We also improve the constructed high-level model by deducing additional information such as data types and constraints. From this enhanced high-level model of code we generate equivalent fragments of code using code generators for Cython and C++ languages. Cython code is seamlessly integrated with Python code, and for C++ code we generate a small utility file in Cython that also integrates this code with Python. This way, the bulk of program code can stay in Python and benefit from its facilities, but performance-critical fragments of code are transformed into more efficient equivalents, improving the performance of resulting program. Comparison of execution times between initial version of Python code, different versions of transformed code and using automatic tools such as Cython compiler and PyPy demonstrates the benefits of our approach – we have achieved performance gains of over 50x compared to the initial version written in Python, and over 2x compared to the best automatic tool we have tested.

Problems in programming 2020; 2-3: 115-125

 


Keywords


improving code performance; rewriting rules technique; Python; Cython

References


Gries, P., Campbell, J. and Montojo, J., 2017. Practical programming: an introduction to computer science using Python 3.6. Pragmatic Bookshelf.

Roghult, A., 2016. Benchmarking Python Interpreters: Measuring Performance of CPython, Cython, Jython and PyPy.

Herron, P., 2016. Learning Cython Programming. Packt Publishing Ltd.

Cython: C-Extensions for Python [Online] Available from: https://cython.org/. [Accessed: 25th February 2020]

PyPy: a fast, compliant alternative implementation of Python [Online] Available from: https://www.pypy.org/. [Accessed: 25th February 2020]

Marowka, A., 2018. Python accelerators for high-performance computing. The Journal of Supercomputing, 74(4). P. 1449-1460. CrossRef

Fua, P. and Lis, K., 2020. Comparing Python, Go, and C++ on the N-Queens Problem. arXiv preprint arXiv:2001.02491.

Doroshenko A., Shevchenko R. A Rewriting Framework for Rule-Based Programming Dynamic Applications. Fundamenta Informaticae. 2006. Vol. 72, N 1-3. P. 95-108.

Termware. [Online] Available from: http://www.gradsoft.com.ua/products/termware_eng.html. [Accessed: 25th February 2020]

Andon P.I., Doroshenko A.Yu., Zhereb K.A., Shevchenko R.S., Yatsenko O.A., Methods of Algebraic Programming. Formal methods of parallel program development. Kyiv, "Naukova dumka". 2017. 440 p.

Doroshenko A.Yu., Zhereb K.A. Algebra-dynamic models for program parallelization. Problems in Programming. 2010. N 1. P. 39-55.

Zhereb K.A. Rule-based software toolset for automating application development on Microsoft .NET Platform. Control systems and machines. 2009. N 4. P. 51-59.

Doroshenko A.Yu., Khavryuchenko V.D., Tulika Ye.M., Zhereb K.A. Transformation of the legacy code on Fortran for scalability and cloud computing. Problems in Programming. 2016. N 2-3. P. 133-140.

Cython Tutorial. [Online] Available from: https://cython.readthedocs.io/en/latest/src/ tutorial/cython_tutorial.html. [Accessed: 25th February 2020]

Rossum van G., Lehtosalo J., Langa Ł. PEP 484 Type Hints [Online] Available from: https://www.python.org/dev/peps/pep-0484/. [Accessed: 25th February 2020]




DOI: https://doi.org/10.15407/pp2020.02-03.115

Refbacks

  • There are currently no refbacks.