Comparing for-loops in R and Python

Python and R are two of the most commonly used high-level programming languages for data analysis. An excellent infographic that compares these languages for data analysis can be found here.

It is sometimes suggested that Python is faster than R, often illustrated using relatively complex examples. However, increasing complexity usually also means an increasing number of ways of optimizing code for better performance, making a direct comparison between the two languages more convoluted. Therefore I was wondering: How do Python and R compare in something as simple as running through a for-loop? To make things as simple as possible, I will compare Python and R in running through an empty for-loop. More specifically, within the body of the loop, nothing will happen at all, so all that’s measured is the actual looping time. In R, for a loop of 10 million iterations, repeated 10 times, this looks as follows:


N <- 1:1E7
nrep <- 10

cat(replicate(nrep, system.time({
	for ( n in N ) { ; }
})[3]), sep = "\n")

In Python, the repeated empty for-loop looks as follows:


import time

N = xrange(0,int(1E7))
nrep = xrange(0,10)

for rep in nrep:
   t0 = time.time()
   for n in N:
      pass
   print time.time() - t0

The results of running these empty for-loops are shown below.

R_vs_Python
Comparing execution times of for-loops of 10 million iterations, repeated 10 times.

Interestingly, R is quite a bit faster than Python! Note of caution: I have only tested this on OS X 10.9.5, using R 3.1.2 and Python 2.7.11, and I cannot exclude the possibility that results would look different under different OSs using different versions of R and Python. Also, to a certain extent, the question of which of these two languages is faster is not very relevant. More specifically, while in general it is important for programming languages to be fast, the primary importance of these two languages is that they are convenient for data analysis, and when it comes to data analysis both languages are great, but just serve slightly different purposes. (Again, for more details, have another look at the infographic.)