Benchmarking Python Interpreters
Total Page:16
File Type:pdf, Size:1020Kb
DEGREE PROJECT IN COMPUTER SCIENCE AND ENGINEERING 300 CREDITS, SECOND CYCLE STOCKHOLM, SWEDEN 2016 Benchmarking Python Interpreters MEASURING PERFORMANCE OF CPYTHON, CYTHON, JYTHON AND PYPY ALEXANDER ROGHULT KTH ROYAL INSTITUTE OF TECHNOLOGY SCHOOL OF COMPUTER SCIENCE AND COMMUNICATION Benchmarking Python Interpreters Measuring Performance of CPython, Cython, Jython and PyPy ALEXANDER ROGHULT Master’s Thesis in Computer Science School of Computer Science and Communication(CSC) Royal Institute of Technology, Stockholm Supervisor: Douglas Wikström Examiner: Johan Håstad Abstract For the Python programming language there are several different interpreters and implementations. In this thesis project the performance regarding execution time is evalu- ated for four of these; CPython, Cython, Jython and PyPy. The performance was measured in a test suite, created dur- ing the project, comprised of tests for Python dictionaries, lists, tuples, generators and objects. Each test was run with both integers and objects as test data with varying prob- lem size. Each test was implemented using Python code. For Cython and Jython separate versions of the test were also implemented which contained syntax and data types specific for that interpreter. The results showed that Jython and PyPy were fastest for a majority of the tests when running code with only Python syntax and data types. Cython uses the Python C/API and is therefore dependent on CPython. The per- formance of Cython was therefore often similar to CPython. Cython did perform better on some of the tests when using Cython syntax and data types, as it could therefore de- crease its reliance to CPython. Each interpreter was able to perform fastest on at least one test, showing that there is not an interpreter that is best for all problems. Referat Jämförelse av Pythoninterpreterarna CPython, Cython, Jython och PyPy Det existerar flera olika implementationer och interpretera- re för programmeringsspråket Python. I detta examensar- bete evalueras prestandan avseende exekveringstid för fyra av dessa; CPython, Cython, Jython och PyPy. Prestandan mättes i en testsvit som skapades i detta projekt. Testsviten bestod av tester för Pythons dictionary, list, tuple, genera- tor och objekt. Varje test kördes med både heltal och objekt som testdata med varierande problemstorlek. Varje test var implementerat i programmeringsspråket Python. För Cyt- hon och Jython implementerades ytterliggare en version av testerna som innehöll syntax och datatyper specifika för dessa interpreterare. Resultaten visade att Jython och PyPy var snabbast för en majoritet av testerna som endast använde sig av Pyt- hons syntax och datatyper. Cython använder sig av Pyt- hons C/API och är därför beroende av CPython. Prestan- dan av Cython var därför lik CPythons. Cython presterade bättre på vissa av testerna som utnyttjade Cythons syntax och datatyper, då den därmed kunde minska sitt beroende av CPython. Varje interpreterare lyckades prestera snab- bast på minst ett test. Detta visar att det inte finns en interpreterare som är mest lämpad för alla problem. Contents 1 Introduction 1 1.1 Purpose .................................. 1 1.2 Motivation . 1 1.3 Limitations . 2 1.4 ProjectPrincipal ............................. 2 2 Background 3 2.1 Python .................................. 3 2.2 Cython .................................. 4 2.3 PyPy.................................... 6 2.4 Jython................................... 7 2.5 PythonDataTypes ........................... 8 2.5.1 List ................................ 8 2.5.2 Tuple ............................... 9 2.5.3 Dictionary . 9 2.5.4 Generators . 9 2.6 Profiling.................................. 9 2.6.1 gtime . 10 2.6.2 cProfile . 10 2.6.3 profilehooks . 10 2.7 Benchmarking............................... 10 3 Method 13 3.1 ProjectBreakdown............................ 13 3.2 Hardware . 14 3.2.1 Mac Pro Intel® Xeon® W3530 2.80 GHz 16 GB RAM . 14 3.2.2 Dell Intel® Core™ i7-5600U CPU @ 2.60GHz 8 GB RAM . 14 3.3 Software Environment . 14 3.4 Profiling.................................. 14 3.4.1 gnu-time . 15 3.4.2 cProfile . 15 3.5 GeneralizingtheProblem ........................ 16 3.5.1 func_d . 18 3.5.2 func_e . 18 3.5.3 func_n-func_q . 18 3.6 Tests.................................... 19 3.6.1 Dictionary . 20 3.6.2 List . 21 3.6.3 Tuple ............................... 22 3.6.4 Generator . 22 3.6.5 Objects .............................. 23 3.7 TestData................................. 23 3.8 InterpreterSpecificCode. 24 4 Results 25 4.1 Discrepancies ............................... 25 4.1.1 Jython . 25 4.1.2 PyPy ............................... 28 4.2 InconsistentResults ........................... 28 4.3 CPython.................................. 31 4.4 Cython .................................. 31 4.5 Jython................................... 31 4.6 PyPy.................................... 32 5 Discussion 33 5.1 Cython .................................. 33 5.2 Jython................................... 34 5.3 PyPy.................................... 35 5.4 Compatibility . 36 5.5 Hardware Dependencies . 37 6 Conclusion 39 7 Future Work 41 7.0.1 Memory Benchmarks . 41 7.0.2 OtherInterpreters ........................ 41 Bibliography 43 Appendices 46 A Cython Code Compilation 47 A.1 List Comprehension Using Python Object . 47 A.2 List Comprehension Using Cython Extension Type . 50 B Decompiled Java Class File Code 53 B.1 Dictionary Insert Using Python Dictionary . 53 B.2 Dictionary Insert Using java.util.HashMap . 53 C PyPy JIT Optimizations 55 C.1 List Comprehension Optimizations . 55 D Charts 59 D.1 DictionaryInsertTests. 59 D.2 Dictionary Merge All Keys Match Tests . 63 D.3 Dictionary Merge Half Keys Match Tests . 68 D.4 Dictionary Merge No Keys Match Tests . 73 D.5 Dictionary Overwrite Tests . 78 D.6 Dictionary Read Tests . 83 D.7 GeneratorTests.............................. 86 D.8 ListAppendTests ............................ 89 D.9 ListComprehensionTests . 93 D.10 List Sort Tests . 96 D.11ObjectsAddTests ............................ 99 D.12ObjectsGenerateTests . 102 D.13 Tuple Append Tests . 104 D.14 Tuple Sort Tests . 107 Chapter 1 Introduction Python is a dynamic programming language that was created in the early 1990s. Python uses an interpreter to execute Python code. Through the years several different interpreters and implementations have been created. In this degree project four of these interpreters will be analyzed and benchmarked in order to determine their performance for different problems. 1.1 Purpose The objective of this degree project is to analyze the variation in performance when using a different Python interpreter than the original CPython implementation. The interpreters that will be evaluated are Cython [1], Jython [2] and PyPy [3]. The analysis will be conducted as described in chapter 3. The scientific question that is aimed to be answered is: How do the different Python interpreters Cython, Jython and PyPy differ in performance compared to CPython and what are the causes for this? 1.2 Motivation Python is a high-level programming language that allows the programmer to trans- late ideas into working code without much difficulty. As with many other program- ming languages that have an abstraction layer, one of the downsides of Python is performance [4]. Over the years, alternative interpreters have been created in at- tempts to maintain Python’s easy syntax while increasing its performance. Perfor- mance evaluation of Python interpreters has already been done by Riccardo Murri [5], the evaluation did not include Jython and involved versions of PyPy and Cython that are now outdated (2.1 and 0.19 respectively). Current versions of PyPy and Cython are 4.0.1 and 0.23.4 which will be used in this project. It is the hypothesis of the author that the interpreters PyPy and Cython in this study will perform faster than CPython for each of the tests, as these interpreters 1 CHAPTER 1. INTRODUCTION were designed for this task. It is, on the other hand, difficult to make assumptions on which interpreter will have the highest performance. 1.3 Limitations In this project the tests that were created were limited to being written in the Python programming language or in the tested interpreters own programming lan- guage. This means that all code was written in Python or Cython. Cython and Jython allow importing modules written in C/C++ and Java respectively, but when implementing the interpreter specific tests this was not done. It is the wish of Tri- Optima to continue using Python and the tests were therefore limited to this. 1.4 Project Principal This degree project was conducted at TriOptima, a global software company de- livering services for the financial sector. One of their products, triReduce, is a multilateral portfolio compression service for OTC derivatives, helping clients man- age post trade risk. A part of triReduce is written in Python 2 and outputs a proposal agreement to be signed by all the participating banks, based on the data in the clients portfolios. A proposal is passed through several verification steps to insure its correctness. As proposals only remain valid for a fixed amount of time due to the market moving, it is important that triReduce can generate a correct proposal within a very short time frame. As client portfolios have grown larger, so has the execution time. As TriOptima wish to continue using Python, it is in their interest to evaluate if using a different interpreter is a viable method in order to increase performance in their systems. 2 Chapter