Speed in search algo in Python and Rust? Ever wondered if the search algorithms that you have learnt in high school or college/university, would perform differently in different programming languages?

Ever wondered if the search algorithms that you have learnt in high school or college/university, would perform differently in different programming languages?

In this article, I will show you how linear and binary search algorithms can be implemented in two programming languages, namely Python and Rust language. Furthermore, the performance comparison between the two algorithms in those two languages.

Before we start on this article, I would like to give a quick shout out to my previous article, https://medium.com/@leonardyeo_xl/evaluating-performance-on-classic-sorting-algorithms-in-python-and-rust-76f981dfc0c, if you are into performance evaluation sort of thing.

Alright, let’s get started!

Before continuing with this article, you should have,

- basic programming experience in Python and Rust and
- basic understanding of some classic search algorithms

Here is the environment that I have used for this evaluation:

- Docker version 19.03.6
- Python 3.7.3
- Rust 1.43.1
- Rust dependencies rand = “0.7.3”
- Rust dependencies floating-duration = “0.1.2”

Just to take note, the algorithms run on a **data size * of *900** elements.

Let’s start coding!

Just a quick review, linear search can be implemented both iteratively and recursively. The data does not have to be sorted for the search to be executed.

**Source Code Iterative**

```
import random
import datetime
def sorted_linear_search(search_key, arr):
for i in range(0, len(arr)-1):
if arr[i] == search_key:
return True
return False
```

**To Run Iterative**

```
if __name__ == "__main__":
array = [index for index in range(1, 901)]
random.shuffle(array)
start = datetime.datetime.now()
result = sorted_linear_search(789, array)
end = datetime.datetime.now()
print("It took: ",(end-start).total_seconds()*1000," ms to search")
```

**Performance Iterative**

It took an estimated **0.024 milliseconds** to search for number 789 in a data size of 900.

binary-search-algorithm rust-programming linear-search python-programming speed

Binary search also known as half-interval search, logarithmic search is an algorithm used in computers to find the position of a value in a sorted array.

A linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found...

Binary Search a sorted array or list by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the...

Guide to Python Programming Language