IronPython vs numpy的Python速度 [英] IronPython vs Python speed with numpy

查看:269
本文介绍了IronPython vs numpy的Python速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Python源代码,可以操纵数字列表(例如大约10,000个浮点数),并对这些数字进行各种计算,例如,包括很多numpy.linalg.norm.

I have some Python source code that manipulates lists of lists of numbers (say about 10,000 floating point numbers) and does various calculations on these numbers, including a lot of numpy.linalg.norm for example.

直到我们最近开始从C#UI使用此代码(通过IronPython从C#运行此Python代码)开始,运行时间才成为问题.我提取了一组函数调用(按照第一段中的描述进行操作),发现与使用Python 2.7相比,此代码在IronPython中运行所需的时间大约是原来的4倍(并且这是在排除了C#/IronPython中的启动/设置时间之后) .我正在使用C#秒表围绕来自C#的重复IronPython调用,并使用timeit模块围绕Python 2.7中的execfile(因此Python时间结果包括更多操作,例如加载文件,创建对象,...而C#却没有').前者大约需要4.0秒,而后者大约需要0.9秒.

Run time had not been an issue until we recently started using this code from a C# UI (running this Python code from C# via IronPython). I extracted a set of function calls (doing things as described in the 1st paragraph) and found that this code takes about 4x longer to run in IronPython compared to Python 2.7 (and this is after excluding the startup/setup time in C#/IronPython). I'm using a C# stopwatch around the repeated IronPython calls from C# and using the timeit module around an execfile in Python 2.7 (so the Python time results include more operation like loading the file, creating the objects, ... whereas the C# doesn't). The former requires about 4.0 seconds while the latter takes about 0.9 seconds.

您希望这种区别吗?有什么想法可以解决这个问题吗?还有其他评论吗?

Would you expect this kind of difference? Any ideas how I might resolve this issue? Other comments?

这是一个简单的代码示例,它在我的机器上的IronPython上运行速度大约慢10倍(在Python 2.7中为4秒,在IronPython中为40秒):

Here is a simple example of code that runs about 10x slower on IronPython on my machine (4 seconds in Python 2.7 and 40 seconds in IronPython):

n = 700
for i in range(n-1):
    for j in range(i, n):
        dist = np.linalg.norm(np.array([i, i, i]) - np.array([j, j, j]))

推荐答案

您正在使用NUMPY吗?您很幸运,它完全可以在IronPython中使用! 在我们说的时候,字面上是在增加支持!

You're using NUMPY?! You're lucky it works in IronPython at all! The support is being added literally as we speak!

确切地说,有一个CPython-extension-to-IronPython接口项目,还有一个本地CLR端口或numpy.我不知道您正在使用哪一个,但是两种方式都比在CPython中使用C版本慢几个数量级.

To be exact, there's a CPython-extension-to-IronPython interface project and there's a native CLR port or numpy. I dunno which one you're using but both ways are orders of magnitude slower that working with the C version in CPython.

更新:

Scipy by Enthought的IronPython端口使用被遗弃的外观:链接的回购协议中的最后提交已使用了几年,在 http://www中丢失了.scipy.org/install.html .从本文的角度来看,它是.NET中具有接口的部分端口,而C中的核心是与自定义接口链接的.上一段也代表它.

The Scipy for IronPython port by Enthought that you're apparently using looks abandoned: last commits in the repos linked are a few years old and it's missing from http://www.scipy.org/install.html, too. Judging by the article, it was a partial port with interface in .NET and core in C linked with a custom interface. The previous paragraph stands for it, too.

使用来自更快的替代方法的信息numpy.argmax/argmin很慢,如果您限制CLR和C内核之间来回传递的数据,则可能会加快速度.

Using the information from Faster alternatives to numpy.argmax/argmin which is slow , you may get some speedup if you limit data passing back and forth between the CLR and the C core.

这篇关于IronPython vs numpy的Python速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆