Friday, December 30, 2011

Matlab vs Python vs Ruby - which one is really faster?

For open source programmers who want to write highly readable and reusable programs, the first choice is Python. It is completely free with a pretty rich set of libraries and tool-chain for almost everything that you can imagine. However we should find how efficient python is when used for writing computational programs.
In this type of computer program, we are typically dealing with giant "for" and "while" loops over repetitive numerical operations such as addition and multiplication etc. Therefore the efficiency of the program is limited by the efficiency of the "for" loop and particularly, the programming language that implements the "for" loop for us.
Here we compare Python with Matlab and Ruby which are usually used. To measure the built-in delay of a for loop we write the following program in Matlab

tic
for i = 1:100000000
    
end
out = toc

and similar program in Python is written as


# File: time-example-5.py
import time
# measure process time
t0 = time.time()
for i in range(0,100000000):
    pass
print time.time() - t0, "seconds process time"

and similar program in Ruby is

now = Time.now
for i in (1..100000000)
end
print Time.now-now

when we run these program, we obtain the following timing. 

                  OUTPUT OF RUBY
--------------------------------------------------------------------
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux]
run1 = 5.970745 seconds
run2 = 6.174075 seconds
run3 = 6.117122 seconds
run4 = 6.028899 seconds
run5 = 6.195276 seconds
--------------------------------------------------------------------

                  OUTPUT OF PYTHON
--------------------------------------------------------------------
Python 2.7.1+ linux32
run1 = 6.72360992432 seconds process time
run2 = 6.64303207397 seconds process time
run3 = 6.67376494408 seconds process time
run4 = 6.68509507179 seconds process time
run5 = 6.83553600311 seconds process time
--------------------------------------------------------------------

                  OUTPUT OF MATLAB
--------------------------------------------------------------------
Matlab 2010 + linux32
run1 = 0.2635 seconds 
run2 = 0.2625 seconds 
run3 = 0.2881 seconds 
run4 = 0.2595 seconds 
run5 = 0.2790 seconds 
--------------------------------------------------------------------

Interestingly, Matlab runs the loop in 0.26 seconds in average which is 23 times better that Ruby and Python versions
Even when we use xrange() in python script to improve performance, the code runs in 2 seconds which is one order of magnitude slower that Matlab code.
We conclude that Matlab performance in "for" loop is much better than Python and Ruby.




2 comments:

  1. i'm pretty sure matlab is optimizing.

    try to actually do something in the loop.

    r = rand
    if r< 0.000001
    print i
    end

    ReplyDelete
    Replies
    1. Yes that's definitely right! and there might be other cases that Matlab wouldn't be able to optimize straightly. However in a long code it is really a pain in the bot to manually optimize and tweak a Python script whatsoever and it is much better choice to leave this task to matlab anyway (in my experience)

      Delete