Evaluating Performance on Search Algorithms in Python and Rust

Evaluating Performance on Search Algorithms in Python and Rust

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?

Backstory

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!

Prerequisites

Before continuing with this article, you should have,

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

Environment

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.

Python

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Binary Search Algorithm in Python (Explained)

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.

Linear Search Algorithm in Python | Data Structure | Searching Algorithm

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 Algorithm in Python | Data Structure | Searching Algorithm

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

Guide to Python Programming Language

Binary Search Recursive Method in Python | Recursive Function | Searching Algorithm

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