Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is MATLAB faster than Python (little simple experiment)

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

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

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

The result was:

Elapsed time is 0.603583 seconds.

x =

    0.947347510922557

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)

I pressed F5 and the result was

49.78125
0.9473475109225565

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

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

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


UPDATE

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.

I repeated the experiments with 1e9 loops.

MATLAB results:

Elapsed time is 5.599789 seconds.
1.643573442831396e-004

PyPy results:

8.609999895095825
0.00016435734428313955

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

Results:

8.218999862670898
0.00016435734428313955

I am going to try NumPy in a little while.

like image 409
rappr Avatar asked May 27 '15 07:05

rappr


People also ask

Is MATLAB faster than Python?

Matlab is faster than Python, but Python is better at running multiple jobs in parallel.

Is MATLAB better than Python?

MATLAB has very strong mathematical calculation ability, Python is difficult to do. Python has no matrix support, but the NumPy library can be achieved. MATLAB is particularly good at signal processing, image processing, in which Python is not strong, and performance is also much worse.

Is Python better than MATLAB for data science?

Python is newer to this arena but is becoming increasingly popular for similar tasks. As you'll see in this article, Python has all of the computational power of MATLAB for science tasks and makes it fast and easy to develop robust applications.

Can Python replace MATLAB?

Python can replace MATLAB The SciPy stack has almost everything you might need for scientific computing, such as numpy for numerical computation, matplotlib for plotting and scikit-learn for machine learning. Python is also one of the easiest programming languages to learn with its readable, simple syntax.


1 Answers

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


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


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)

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

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

And there are always new projects on the horizon as well.

like image 69
abarnert Avatar answered Oct 10 '22 13:10

abarnert