本文介绍了与此算法的 Java 相比,Python 非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究算法并决定将 Java Programs 从教科书移植到 Python,因为我不喜欢 Java 开销,尤其是对于小程序,并作为练习.

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.

算法本身非常简单,它只是以一种蛮力的方式从数组中取出所有三元组,并计算有多少三元组总和为零(例如:[-2,7,-5])

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;
    }

我把它移植到:

def count(a):
    cnt = 0
    ln = 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+=1
    return cnt

现在只测量这些功能:

java :   array of 2000 elements --> 3 seconds
python : array of 2000 elements --> 2 minutes, 19 seconds

UPDATE
python (pypy) : array of 2000 elements --> 4 seconds ( :-) )

当然这不是一个好的算法,它只是用来展示,无论是在这里还是在教科书中.我之前用 Java 和 Python 做过一些编程,但没有意识到这种巨大的差异.

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. 这段代码是一个很好的移植,还是我遗漏了一些微不足道的东西?
  2. 例如切换到另一个运行时 Jython 是一种解决方案吗?将我的代码库保存在 eclipse 中并只添加一个解释器(编译器?)是否容易?还是切换到另一个解释器/编译器只会让事情稍微好一点?

现在我在 Windows 7 上使用 python 2.7.3 和 Java 1.7 32ibts.

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

我知道关于 java/python 性能的 SO 上也有类似的问题,但是目前关于 python 有不同的运行时环境之类的答案对我没有帮助.

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?

更新:

我安装了 pypy,现在差异很大...

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

更新 2:

我注意到一些非常有趣的事情:这里答案中的 islice 方法在常规"python 上速度更快,但在 pypy 上要慢得多.即便如此,pypy 在此算法中无论使用常规循环还是使用 islices 仍然要快得多

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

正如 Bakuriu 在评论中注意到的,运行时环境可能非常重要,但是对于此算法而言更快的运行时环境对于任何算法来说都不一定更快......

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...

推荐答案

尝试使用 PyPy 而不是 CPython 来运行它.它很可能会走得更快.

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

这篇关于与此算法的 Java 相比,Python 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 13:35