Python very slow as compared to Java for this algorithm

2024/11/17 15:59:50

I'm studying algorithms and decided to port the Java Programs from the textbook to Python, since I dislike the Java overhead, especially for small programs, and as an exercise.

The algorithm itself is very simple, It just takes all triplets out of an array, in a bruteforce kinda way, and counts how many of the triplets sum up to zero (eg: [-2,7,-5])

 public static int count(int[] a) {int N = a.length;int cnt = 0;for (int i = 0; i < N; i++) {for (int j = i+1; j < N; j++) {for (int k = j+1; k < N; k++) {if (a[i] + a[j] + a[k] == 0) {cnt++;}}}}return cnt;} 

I ported it to :

def count(a):cnt = 0ln = len(a)for i in xrange(0,ln): for j in xrange(i + 1,ln):for k in xrange(j + 1,ln): if a[i] + a[j] + a[k] == 0:cnt+=1return cnt

Now measuring just these functions are taking :

java :   array of 2000 elements --> 3 seconds 
python : array of 2000 elements --> 2 minutes, 19 secondsUPDATE 
python (pypy) : array of 2000 elements --> 4 seconds ( :-) )

Of course this is not a good algorithm, it just goes to show, both here and in the textbook. I have done some programming both in Java and Python before, but was not aware of this huge difference.

The question boils down to : how te overcome this? More specifically :

  1. Is this code a good port, or am I missing something trivial?
  2. Is switching to another runtime Jython for example a solution? Is it easy to keep my codebase in eclipse and just add an interpreter (compiler?) ? Or will switching to another interpreter/compiler only make things slightly better?

Right now I am using python 2.7.3 and Java 1.7 32ibts on windows 7.

I know there are similar questions out there on SO about java/python performance, but the answers like there are different runtime environments for python out there are not helpfull for me at the moment.

What I want to know is if some of these runtimes can close this huge gap and are worth epxloring?

UPDATE :

I installed pypy and the differences now are enormous...

UPDATE 2 :

Some very interesting things I noticed : the islice method in an answer here is faster on 'regular' python, but a lot slower on pypy. Even so, pypy still remains a lot faster using no matter it uses regular loops or islices in this algoritm

As Bakuriu notices in a remark runtime environments can matter a whole lot, but a runtime environment faster for this algoritm is not necessarily faster for any algoritm...

Answer

Try running it with PyPy instead of CPython. It will very likely go much faster.

https://en.xdnf.cn/q/71202.html

Related Q&A

fd.seek() IOError: [Errno 22] Invalid argument

My Python Interpreter (v2.6.5) raises the above error in the following codepart:fd = open("some_filename", "r") fd.seek(-2, os.SEEK_END) #same happens if you exchange the second arg…

When is a variable considered constant in terms of PEP8 naming styles?

In keeping with PEP8 conventions, in a .py I can define constants as:NAME = "Me" AGE = "Old" GENER = "Male"If a .txt contained Me Old Male on a single line, and in anothe…

Average Date Array Calculation

I would like to get the mean of the following dates. I thought about converting all the data to seconds and then averaging them. But there is probably a better way to do it.date = [2016-02-23 09:36:26,…

DRF: Serializer Group By Model Field

I want my api to return Account objects grouped by the account_type field in the model. I want to do this so that the grouped accounts are easier to access in my JS code. This is what is returned now:[…

Need help combining two 3 channel images into 6 channel image Python

I am trying to combine two different RGB images into a single 6 channel image (Tiff is best) using nothing but Python.What I have is an RGB image taken from a normal camera as well as another RGB image…

Komodo Edit disable autocomple

I am using Komodo Edit 8 and its autocomplete feature is totally annoying. As soon as I type "for i" , it autofills in this:for i in range:codeNow i have to delete it manually to continue typ…

Python WeakKeyDictionary for unhashable types

As raised in cpython issue 88306, python WeakKeyDictionary fails for non hashable types. According to the discussion in the python issue above, this is an unnecessary restriction, using ids of the keys…

Django MPTT efficiently serializing relational data with DRF

I have a Category model that is a MPTT model. It is m2m to Group and I need to serialize the tree with related counts, imagine my Category tree is this:Root (related to 1 group)- Branch (related to 2 g…

Psycopg2: module object has no attribute connect [duplicate]

This question already has answers here:Importing a library from (or near) a script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError(4 answers…

matplotlib text not clipped

When drawing text in matplotlib with text(), and then interactively panning the image, the resulting drawn text is not clipped to the data window. This is counter to how plotting data or drawing text …