MATLAB是否比Python快(简单的实验) [英] Is MATLAB faster than Python (little simple experiment)

查看:1086
本文介绍了MATLAB是否比Python快(简单的实验)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过这篇文章( MATLAB是否比Python快?),我发现它有很多ifs.

I have read this ( Is MATLAB faster than Python? ) and I find it has lots of ifs.

我已经在仍然可以在Windows XP上运行的旧计算机上尝试了这个小实验.

I have tried this little experiment on an old computer that still runs on Windows XP.

在MATLAB R2010b中,我将以下代码复制并粘贴到命令窗口中:

In MATLAB R2010b I have copied and pasted the following code in the Command Window:

tic
x = 0.23;
for i = 1:100000000
  x = 4 * x * (1 - x);
end
toc
x

结果是:

Elapsed time is 0.603583 seconds.

x =

    0.947347510922557

然后,我使用以下脚本保存了 py 文件:

Then I saved a py file with the following script:

import time
t = time.time()
x = 0.23
for i in range(100000000): x = 4 * x * (1 - x)
elapsed = time.time() - t
print(elapsed)
print(x)

我按了 F5 ,结果是

49.78125
0.9473475109225565

在MATLAB中花费了0.60秒;在Python中花了49.78秒(永恒!).

In MATLAB it took 0.60 seconds; in Python it took 49.78 seconds (an eternity!!).

所以问题是:是否有一种简单的方法可以使Python像MATLAB一样快?

So the question is: is there a simple way to make Python as fast as MATLAB?

特别是:如何更改py脚本,使其运行速度与MATLAB一样快?

Specifically: how do I change my py script so that it runs as fast as MATLAB?

更新

我曾在 PyPy 中尝试过相同的实验(复制和粘贴与上面相同的代码):它在1.0470001697540283秒内与以前在同一台计算机上完成了此操作.

I have tried the same experiment in PyPy (copying and pasting the same code as above): it did it in 1.0470001697540283 seconds on the same machine as before.

我用1e9循环重复了实验.

I repeated the experiments with 1e9 loops.

MATLAB结果:

Elapsed time is 5.599789 seconds.
1.643573442831396e-004

PyPy结果:

8.609999895095825
0.00016435734428313955

我也尝试了正常的while循环,结果相似:

I have also tried with a normal while loop, with similar results:

t = time.time()
x = 0.23
i = 0
while (i < 1000000000):
    x = 4 * x * (1 - x)
    i += 1

elapsed = time.time() - t
elapsed
x

结果:

8.218999862670898
0.00016435734428313955

我将在一段时间内尝试 NumPy .

I am going to try NumPy in a little while.

推荐答案

首先,使用time并不是测试此类代码的好方法.但是,让我们忽略它.

First, using time is not a good way to test code like this. But let's ignore that.

如果您的代码每次循环都要进行很多循环并重复非常相似的工作,那么 PyPy 的JIT会做得很好工作.当该代码每次完全相同时,将其常量化为可以从循环中取出的常量值时,它将做得更好.另一方面,CPython必须为每次循环迭代执行多个字节码,因此它会很慢.通过在我的计算机上进行的快速测试,CPython 3.4.1耗时24.2秒,而PyPy 2.4.0/3.2.5耗时0.0059秒.

When you have code that does a lot of looping and repeating very similar work each time through the loop, PyPy's JIT will do a great job. When that code does the exact same thing every time, to constant values that can be lifted out of the loop, it'll do even better. CPython, on the other hand, has to execute multiple bytecodes for each loop iteration, so it will be slow. From a quick test on my machine, CPython 3.4.1 takes 24.2 seconds, but PyPy 2.4.0/3.2.5 takes 0.0059 seconds.

IronPython和Jython也是JIT编译的(尽管使用了更通用的JVM和.NET JIT),因此在这种工作上,它们也往往比CPython快.

IronPython and Jython are also JIT-compiled (although using the more generic JVM and .NET JITs), so they tend to be faster than CPython for this kind of work as well.

通常,您还可以通过使用 NumPy 数组和矢量运算而不是Python列表和循环来加快CPython本身的工作速度.例如,下面的代码花费0.011秒:

You can also generally speed up work like this in CPython itself by using NumPy arrays and vector operations instead of Python lists and loops. For example, the following code takes 0.011 seconds:

i = np.arange(10000000)
i[:] = 4 * x * (1-x)

当然,在那种情况下,我们明确地只计算一次该值并将其复制10000000次.但是我们可以强制它一次又一次地进行实际计算,它仍然只需要0.12秒:

Of course in that case, we're explicitly just computing the value once and copying it 10000000 times. But we can force it to actually compute over and over again, and it still takes only 0.12 seconds:

i = np.zeros((10000000,))
i = 4 * (x+i) * (1-(x+i))


其他选项包括在 Cython (针对Python编译为C扩展)中编写部分代码,以及使用


Other options include writing part of the code in Cython (which compiles to a C extension for Python), and using Numba, which JIT-compiles code within CPython. For toy programs like this, neither may be appropriate—the time spent auto-generating and compiling C code may swamp the time saved by running C code instead of Python code if you're only trying to optimize a one-time 24-second process. But in real-life numerical programming, both are very useful. (And both play nicely with NumPy.)

总是有新项目出现.

这篇关于MATLAB是否比Python快(简单的实验)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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