1

I have a Python program that I'm using to processed large sets of images (100k+). So even the smallest performance penalties (cited below) have a huge impact.

This program originally was written in Python2, after translating it to Pythor3 it got noticeably slower (~15%). Here I will present a part of code, but the same behavior is repeated all through the rest of the program.

I used Line Profiler (https://github.com/pyutils/line_profiler) to profile two versions of code I got these results:

Python2 (2.7.15)

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    74                                               @profile
    75                                               def findPoint(self, scale,rho):
    76    118500     106586.0      0.9     35.8          x, y = scale*self.maxRad*cos(rho), scale*self.minRad*sin(rho)
    77    118500     143277.0      1.2     48.1          x2, y2 = self.xImage+x*cos(self.thetaImage)-y*sin(self.thetaImage), self.yImage+x*sin(self.thetaImage)+y*cos(self.thetaImage)
    78    118500      48026.0      0.4     16.1          return x2, y2

Python3 (3.9.1)

    74                                               @profile
    75                                               def findPoint(self, scale,rho):
    76    118500     149387.0      1.3     39.6          x, y = scale*self.maxRad*cos(rho), scale*self.minRad*sin(rho)
    77    118500     174674.0      1.5     46.3          x2, y2 = self.xImage+x*cos(self.thetaImage)-y*sin(self.thetaImage), self.yImage+x*sin(self.thetaImage)+y*cos(self.thetaImage)
    78    118500      52992.0      0.4     14.1          return x2, y2

An example is showing that Python2 is showing better performance on the same data and code segment. Other parts are showing the same.

The internet is telling me that Python3 is faster, so maybe I need to optimize something?

OBS: This code is also compiled with Cython and showing the same dynamics of performance decrease with Python3.

Igor Kolesnikov
  • 133
  • 1
  • 8
  • 1
    CPython 3 is an interpreter and thus not built for high performance. 15% is a pretty small difference and is likely to be due to low-level implementation detail changes in the interpreter (change in data structure, more computation due to added feature in the language, etc.). If a 15% performance gap is very important to you, your are probably better off using Cython (or alternatives like this). – Jérôme Richard Apr 07 '21 at 17:29
  • 1
    Does any of this involve huge integers? Python 3 uses arbitrary-precision integers that can grow to unbounded sizes. Python 2 uses fixed-width int by default. – Peter Cordes Apr 07 '21 at 19:01

0 Answers0