Is MATLAB faster than Python (little simple experiment)
Question: I have read this ( [Is MATLAB faster than
Python?](http://stackoverflow.com/questions/2133031/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.


Answer: 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](http://pypy.org)'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](http://numpy.org) 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](http://cython.org)
(which compiles to a C extension for Python), and using
[Numba](http://numba.pydata.org), 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.

