Why Python3 is slower than Python?

137 Views Asked by At

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.

0

There are 0 best solutions below