Simulatin speed: GNU R vs Julia

November 22, 2013


Recently there is a lot of noise about Julia. I have decided to test its speed in simulation tasks on my toy Cont model. I thought I had vectorized my GNU R code pretty well, but Julia is much faster.

The model was described in my earlier posts so let us go down to a comparison:
Here is my GNU R code:

library(e1071) <- function(, reps, n, d, l, s) {
  tr <- rep(0, n)
  r <- rep(0, reps)
  for (i in 1:reps) {
    sig <- rnorm(1, 0, d)
    mul <- 1
    if (sig < 0) {
        sig <- -sig
        mul <- -1
    r[i] <- mul * sum(sig > tr) / (l * n)

system.time(replicate(10,, 10000, 1000, 0.005, 10.0, 0.01)))

It's execution time is a bit below 10 seconds on my laptop.

An equivalent Julia code is the following:

using Distributions

function cont_run(burn_in, reps, n, d, l, s)
    tr = Array(Float64, n)
    r = Array(Float64, reps)
    for i in 1:reps
        aris = 0
        sig = randn() * d
        mul = 1
        if sig < 0
            sig = -sig
            mul = -1
        for k in 1:n
            if sig > tr[k]
                aris += 1
        ari = aris / (l * n)
        r[i] = mul * ari
        for j in 1:n
            if rand() < s
                tr[j] = ari

n = 10
t_start = time()
k = Array(Float64, n)
for i in 1:n
    k[i] = cont_run(1000, 10000, 1000, 0.005, 10.0, 0.01)
println(time() -t_start)

And on my machine it takes a bit less than 0.7 seconds to run.

So we get over tenfold speedup. This is a significant difference for simulation experiments.
I will have to dig more into Julia in the future.



Comments are closed.