Introduction to Julia

Julia was born in the not so distant year 2012 and yet it is already among the top 50 most popular programming languages according to TIOBE Index.
Julia was created by Jeff Bezanson, Stefan Karpinski, Viral B. Shah, and Alan Edelman.
With their own words the reasons behind the creation of Julia were:

We want a language that’s open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled.” (read it all on the creators’ blog)

Sounds ambitious but it looks like they did it. A quite impressive last stable version (1.3.1) was released on the 30th of December 2019. The young Julia is gaining fans quickly and some predict it might even dethrone Python as the hottest data analytics language. We’ll leave this for the future generations to decide.

Now let’s go through some notable features of Julia:

• Julia is open source with an MIT license and is free;
• It combines the interactivity of scripting languages like Python and Matlab with the speed of compiled languages such as Fortran and C;
• Julia is a dynamic programming language but allows type declaration. Тypes are themselves run-time objects. Operators are just functions with special notation and functions are defined on different combinations of argument types, and applied by dispatching to the most specific matching definition; Multiple dispatch is a core programming paradigm in Julia;
• Julia is compiled not interpreted. It is just-in-time (JIT) compiled using the LLVM compiler framework which makes it fast (as fast as C);
• Julia supports metaprogramming capabilities. The programs can create programs and change the source code.
• Designed for parallelism and distributed computation;
• Designed for fast math;
• Includes interactive command line – REPL (read-eval-print loop) similarly to Python;
• You can call Python, C, and Fortran libraries;
• Syntax is similar to Python;

Overview of  some key features in comparison to Python:

• Performance: Julia is fast by design – while Python’s code can certainly be optimised (using 3rd party JIT compilers like PyPy or using the Cython package) it remains an interpreted language which gives Julia the advantage as a compiled language when it comes to performance.
• Parallelism: Both Julia and Python are capable of running parallel operations however Python often requires serialising and deserialising of data between nodes which makes Julia the superior language in terms of parallelism.
• Community: We can say many nice things but the facts Julia is still a baby with relatively small community. Python’s massive support and long history make it the clear winner here.
• Packages and libraries: When we speak about the maturity of a language it’s worth mentioning the role that plays in the development of various libraries. Again Python wins here with the many 3rd party packages available. Though we should mention that Julia supports the direct use of some Python and C libraries.

We have to agree that taking down Python would be a difficult thing to do and while Julia has great potential it still needs to mature as a language.

Having said that, the industries and especially researchers are already applying Julia. The Federal Reserve Bank of New York has made economy models using Julia as a substitute of their previous Matlab implementation resulting in ten times faster modelling.
Julia Computing and NVIDIA have announced the availability of the Julia programming language as a pre-packaged container on the NVIDIA GPU Cloud (NGC) container registry.
The Climate Modelling Alliance has chosen Julia as the sole language on which their global climate model will be implemented.
The researchers from the Celeste project have been using Julia on Cori II (Cray XC40) supercomputer (the 6th fastest computer in the world) to speed astronomical image analysis 1000 times and catalog 188 million astronomical objects in 15 minutes.

 

After this quick overview we are ready to try out some real Julia.

We mentioned multiple times how fast this language is. Let’s check it!

But before we can move to some actual code we need to go through the easy process of installing Julia.
From https://julialang.org/downloads/  download the version for your operating system.
Once finished, open the REPL (Julia’s command-line).You can type directly there:

Julia programming language

Or you can use a Jupyter notebook. To do that you need to install the package from REPL:

using Pkg

Pkg.add(“IJulia”)

Julia programming language 2

Now you can open a new jupyter notebook and use the Julia kernel (assuming you already have Anaconda installed).

 

For our speed test we are going to use a simple summing function :

Simple function

where n is the length of a and a is the vector  a = rand(10^7of random numbers uniformly distributed in [0,1).

We are going to run Python code within Julia, store the execution time and then compare it the result from execution in Julia.

We are using the PyCall library to run Python directly in the Julia notebook. Here is the hand-written function in Python:

# Python code
py"""
def py_sum(A):
    s = 0.0
    for a in A:
        s += a
    return s
"""

sum_py = py"py_sum"

The execution time:

execution time of julia programming in python notebook

We can use try to use the numpy library and it’s built-in sum function:

using Conda
Conda.add("numpy")

numpy_sum = pyimport("numpy")["sum"]

py_numpy_bench = @benchmark $numpy_sum($a)

This time the calculation took:

calculation time

Using numpy improved the performance. Now it’s time to test Julia.

function mysum_simd(A)
    s = 0.0 # s = zero(eltype(A))
    @simd for a in A
        s += a
    end
    s
end

Julia programming test

Julia is performing 2.5 times faster than numpy and over 450 times faster than the hand-written python!

You can check the entire code for the performance test in this notebook as well as some basic concepts of the language like data structures, conditional statements, etc.

If you are interested in learning more – try it yourself! I recommend the tutorials on the official page https://julialang.org/learning/  they are free and easy to follow.

 

Overall, I believe Julia has a bright future ahead. Julia is fun to write is already a serious competitor to Python when it comes to numerical and scientific computing.

It will be interesting to see how the language progresses.

 

Refferences:

https://docs.julialang.org/en/v1/

https://juliacomputing.com/case-studies/celeste.html

https://www.infoworld.com/article/3241107/julia-vs-python-which-is-best-for-data-science.html

https://www.nature.com/articles/d41586-019-02310-3

https://en.wikipedia.org/wiki/Julia_(programming_language)