2.1 Julia is fast

Once upon a time I wrote these three time consuming programs (so hold your horses, you may not want to run them):

# file: test.jl
for i in 1:1_000_000_000
    if i == 500_000_000
        println("Half way through. I counted to 500 million.")
    end
end
println("Done. I counted to 1 billion.")
# file: test.py
for i in range(1_000_000_000):
    if i == 500_000_000:
        print("Half way through. I counted to 500 million.")
print("Done. I counted to 1 billion.")
# file: test.r
for (i in 1:1000000000) {
  if (i == 500000000) {
    print("Half way through. I counted to 500 million.")
  }
}
print("Done. I counted to 1 billion.")

Note: Python and Julia allow to write numbers either like this: 1000, or like that 1_000. The latter form uses _ to separate thousands, so more typing, but it is more legible.

Each program counts to 1 billion (1 with 9 zeros). Once it is half way through it displays an info on the screen and when it is done counting it prints another message.

The execution times of the scripts on my few-years old laptop (the specification is not that important):

  1. Julia: ~1.5 [sec]
  2. R: ~33 [sec]
  3. Python3: ~50 [sec]

Granted, it’s not a proper benchmark, and e.g. Python’s numpy library runs with the speed of C (so a bit faster than Julia). Nevertheless, the code that I write in Julia is consistently ~5-10 times faster than the code I write in the other two programming languages. This is especially evident when running computer simulations like the ones you may find in this book, still, it is just a subjective feeling.

Fun fact: A human being would likely need more than 32 years to count to 1 billion. Test yourself and show why. Hint: try to estimate for how long you are alive [in seconds].



CC BY-NC-SA 4.0 Bartlomiej Lukaszuk