top of page

Julia Programming Language - An Introduction

  • Writer: Stefano Giannini
    Stefano Giannini
  • Aug 15, 2022
  • 3 min read

1. What is Julia?

Julia is a dynamic, high-level, high-performance programming language. Its aim is to provide a flexible dynamic language, appropriate for scientific and numerical computing, with performance comparable to traditional statically-typed languages (like C/C++). Therefore Julia is also suitable for Big Data analysis and fast prototyping of production applications.


Some of the advantages of Julia language are:

  • Free and open source (MIT licensed)

  • User-defined types are as fast and compact as built-ins

  • No need to vectorize code for performance; devectorized code is fast

  • Designed for parallelism and distributed computation

  • Lightweight "green" threading (co-routines)

  • Unobtrusive yet powerful type system

  • Elegant and extensible conversions and promotions for numeric and other types

  • Efficient support for Unicode, including but not limited to UTF-8

  • Call C functions directly (no wrappers or special APIs needed)

  • Powerful shell-like capabilities for managing other processes

  • Lisp-like macros and other meta-programming facilities


2. Why should I learn it?

If you are a Data Scientist or someone who needs scientific computing in its job, you should consider Julia as a valuable option to Python or R. Especially if one of your main concern is speed and efficiency.

Moreover, since Julia is dynamic, also the speed of programming is quite good if compared to other languages with similar execution speed (C/ C++/ FORTRAN)



3. How to install Julia

3.1. Windows

  • Download the installer for your platform (in general 64-bit installer for the stable version should be fine)

  • Run the installer

  • Be sure to check the "Add Julia to PATH" in order to automatically add Julia to the environment variables.

3.2. Linux and other platforms

Check the official tutorial



4. Hello World!

We can both use the interactive console and a "*.julia" file. In this case it will be shown the interactive session. If you have properly installed Julia, you should access the session from the console (or Command Prompt) typing julia.


Since this programming language is dynamic we can write an "Hello World!" program very easily in multiple ways.

$ "Hello World!"    # Simply type a string on the console
"Hello World!"
$ # use the print() function, note the absence of the double quotes
$ print("Hello World!")  
Hello World!
$ # use the println() function, it will end with a new line
$ println("Hello World!")    
Hello World!

$ ...


5. Variables and Data Types

5.1. Variables

Every name associated with a value is a variable in Julia. The type of a variable can be reassigned multiple times like in Python. If you want to remove the automatic output in the interactive console you should end the line with a semicolon ";".


$ x = 10
10
$ x + 1
11
$ x = 1 + 2
3
$ x = "String"
"String"
$ x = sqrt(100);    # no output in the console with a semicolon
$ # complex numbers
$ 1 + 1im
1 + 1im
$a=1; b=2; complex(a,b)
1 + 2im
$ δ=5             # use special character (\delta - Tab key)
5

5.2. Data Types

Since it is a dynamic language sometimes there is no reason to declare the data types explicitly. However, for speed reason, it could be useful to specify data type.


Integer types:

  • Int8: [-128,127]

  • Int16: [-32768,32767]

  • Int32: [-2147483648,2147483647]

  • Int64: [-9223372036854775808,9223372036854775807]

  • Int128: [-170141183460469231731687303715884105728, 170141183460469231731687303715884105727]

  • UInt8: [0,255]

  • UInt16: [0,65535]

  • UInt32: [0,4294967295]

  • UInt64: [0,18446744073709551615]

  • UInt128: [0,340282366920938463463374607431768211455]

Float types:

  • Float16: [-Inf,Inf]

  • Float32: [-Inf,Inf]

  • Float64: [-Inf,Inf]

Then there are String, Char and Boolean types.

Let's see some examples:


# in a 32/64-bit system the default Float and Int size is 32/64.
$ typeof(123)
Int64 
$ typeof(4.4)
Float64
# however for big numbers the representation can be only in 64-bit
$ typeof(10000000000)
Int64

# for other types we can declare with hexadecimal values or local variables
$ typeof(0x1)
UInt8
$ x = Int8(10)        # actually converts 10 from Int64 to Int8
10
$ typeof(x)
Int8

For composite and primitive types, check out the official manual.



6. Speed Comparison vs. Python

We will show a benchmark using a simple program to compute Fibonacci sequence.


Python code:

import time

def fibb(n):
    if (n<=1): return n 
    else: return (fibb(n-1) +fibb(n-2))

for i in range(25): 
    start_time=time.time()
    fibb(30)
    end_time=time.time()
    print("%s"% (end_time-start_time))

Julia code:

function fibb(n)
    if (n<=1) 
        return n
    else 
        return (fibb(n-1) +fibb(n-2))
    end
end

for i in 1:25 
    @time fibb(30)
end

On my PC with a Ryzen 7 5700U (8GB of RAM) I get approximately:

  • 0.55s in Python

  • 0.009s in Julia

The Julia code is roughly 60 times faster. The difference further increase for higher value of Fibonacci sequence. For n=50 the python code can not even complete the calculation (since it took more than 15 minutes.




Comments


bottom of page