[R] python

Stefan Evert stefan.evert at uos.de
Sat Nov 21 23:32:08 CET 2009


> My hunch is that Python and R run at about the same speed, and both
> use C libraries for speedups (Python primarily via the numpy package).

That's not necessarily true.  There can be enormous differences  
between interpreted languages, and R appears to be a particularly slow  
one (which doesn't usually matter, as well-written code will mostly  
perform matrix operations).

I did run some simple benchmarks with "naive" loops such as this one

> for (x in 1:N) {
>         sum <- sum + x
> }

as well as function calls.  I haven't tested Python yet, but in  
generally it is considered to be roughly on par with Perl.

Here are results for the loop above:

R/simple_count.R                   0.82 Mops/s  (2000000 ops in 2.43 s)
perl/simple_count.perl             8.32 Mops/s  (10000000 ops in 1.20 s)

(where Mops = million operations per second treats one loop iteration  
as a single operation here).  As you can see, Perl is about 10 times  
as fast as R.  The point is, however, that this difference may not be  
worth the effort you spend re-implementing your algorithms in Python  
or Perl and getting the Python/Perl interface for R up and running  
(I've just about given up on RSPerl, since I simply can't get it to  
install on my Mac in the way I need it).

The difference between R and Perl appears much less important if you  
compare it to compiled C code:

C/simple_count.exe               820.86 Mops/s  (500000000 ops in 0.61  
s)

If you really need speed from an interpreted language, you could try  
Lua:

lua/simple_count.lua              65.78 Mops/s  (100000000 ops in 1.52  
s)

(though you're going to lose much of this advantage as soon as you  
include function calls, which have a lot of overhead in every  
interpreted language.


Hope this helps,
Stefan




More information about the R-help mailing list