# Python vs Julia: Speed Test on Fibonacci Sequence

Recently MIT released a course on Computational Thinking with code 18.S191 and it is available on YouTube. I can code in C++ and Python, so the founder’s claim that this code is as fast as C and as easy as Python gains my interest.

## Introduction to Julia

Julia is created in 2009 and first introduced to public in 2012. The developers aimed for scientific computing, machine learning, data mining, and large-scale linear algebra. We might have heard this application on Python, but Julia gives advantages to programmer compared to Python.

1. Unlike Python, an interpreted language, Julia is a compiled language. It is a just-in-time (JIT) compiled using the LLVM compiler framework. Thus, Julia can go as fast as the speed of C language.
2. Julia is as interactive as Python, it includes a REPL (read-eval-print loop) or interactive command line. You can easily run Julia code from your command line.
3. Julia is easy to read, the syntax is straightforward just like Python’s. With its web-based IDE, programmer can even use a Greek symbol in their code.
4. Julia has both dynamic typing and static typing. Programmers can specify types of variables or let the computer sign the types for you. When writing a function, programmers can also specify the input variable type or leave it unspecified.
5. Julia can call C, Fortran, and Python libraries. This programming language can call directly with libraries written in C and Fortran. It can also call Python code by using PyCall library.

## Fibonacci Sequence

Fibonacci is one of the most common function in programming to introduce recursion. In short, a Fibonacci number is a sum of it’s two preceding ones, that start from 0 and 1.

In Python, one can write the Fibonacci Sequence as shown below.

```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))
```

The code below shows how the Fibonacci sequence is written in Julia. Pretty similar to Python, isn’t it? Note that the for loop is used to evaluate the execution time repeatedly.

```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
```

Both functions will return the n-th number of the Fibonacci Sequence. This problem is known to have an exponential time consumption. So, checking the speed using this function will be interesting

## Speed Test

I run both function from terminal. One can simply type “julia fibb.jl” to execute the Julia code and similarly type “python fibb.py” to run the Python one.

I run the Fibonacci function for both 5 test cases, finding the 10th, 20th, 30th, 40th, and 50th Fibonacci number. For each test cases, the function is executed 25 times and the average is calculated.

The result is shown from the images above. It is clear that Julia has a better performance in calculating Fibonacci sequence. Since the 10th term calculation, Julia has scored 17 times faster than Python. For calculating the 40th term, Julia is 71 times faster. Seventy One!

On calculating the 50th term, Julia took 70 seconds while Python took forever. I left the program running for more than 10 minutes and it is not finished.

As a side note, I run the codes on my personal computer, with Core i7 8th Gen processor.

# Conclusion

On calculating the Fibonacci sequence, Julia has been proven to be superior than Python. This means for a simple mathematical operation, in general, Julia is faster. There are many more comparison programmers can do to decide which one has a better performance.

However, since Julia is developed just recently, it still has a limited libraries and communities support compare to Python. So, you can contribute a lot to Julia development. I do not recommend Julia as a language to learn programming the first time since you might get less help from others.