On 11/21/2009 11:32 PM, Stefan Evert wrote:

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
}

Sure, badly written R code does not perform as well as well written python code or C code. On the other hand badly written python code does not perform as well as well written R code.

What happens when you try one of these :

sum <- sum( 1:N )
sum <- sum( seq_len(N) )
sum <- N * (N + 1L) / 2L  # ;-)

A lot can be done by just rewriting some of the R code.


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

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/EAD5 : LondonR slides
|- http://tr.im/BcPw : celebrating R commit #50000
`- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to