"is" & "==" in Python. Speed up string comparison
By: Sivasai Yadav Mudugandla
Tags: Python Programming, Python3, Coding, Programming, Python

Speed up your string comparisons

Many programmers and beginners love python. Python is one of the languages that is witnessing incredible growth and popularity year by year.

In 2017, Stack overflow calculated that python would beat all other programming languages by 2020 as it has become the fastest-growing programming language in the world.

Python language is one of the most accessible programming languages available because of its Readability, tons of libraries, Vibrant Community, can be used for Web Programming, Desktop Applications, Big data, Cloud Computing, Machine Learning, and so on.

It’s very important for every Pythonista to understand the difference between == (equality operator ) and is ( identity operator ) before you use it.

But, many of us are confused about which one to use in which situation. Let’s understand it clearly.

In this article, I will discuss

What is equality and identity operators When to use equality and identity operators Which one is best.

1. What is == and is operators

When var1 & var2 variables are created, Memory will be allocated as above

The ‘is’ operator may seem like the same as the == (equality operator) but they are not the same. The ‘is’ is an identity operator used to checks whether two variables point to the same object in memory or to compare the identity of two objects. It returns True if the operands are identical (refer to the same object).

Let’s see with an example,

Above code returned False because the two variables pointing to the different objects i.e. different memory addresses (0x7fce1afef330 & 0x7fce1afef050). To get the memory addresses of var1 & var2 use hex(id(var1)) and hex(id(var2)).

On the other hand, the == operator checks if the values for the two variables are the same. It returns True if both operands are equal.

As expected, the above code returned True because the contents of the two variables are equal.

2. When to use == and is operators

As a rule of thumb, Use the == operator when your primary objective is to compare the contents/values of two objects and you don’t care about where they’re stored in memory. Use the is operator only when you want to check whether two variables are pointing to the same memory address. When you perform a string comparison operation with the ‘==’ operator, python compares character by character.

The string(‘Abc’) used in the above example is very small. But what if we have a long string? Python still compares character by character. Won’t it be a slow process? Instead of comparing content, we can compare their memory addresses which are just integers. So, comparing integers are far better than comparing two long strings character by character. This is much faster and less CPU utilization than comparing character by character.

NOTE: Python points variables to the same memory address when they hold the same content. Not all strings are automatically interned in python. But, we can force strings to be interned by using the sys.intern().

So, Use ‘is’ (identity) operator than ‘==’ (equality) operator to speed up your heavy string comparison operations.

3. Which one is best

This is definitely an individual’s choice, but if you talk about speed ‘is’ (identity) beats ‘==’ (equality) operator.

Let’s check with code which one is actually best in terms of speed.

== operator execution time :

import time def equality_operator(n):

a = ‘Which one is best? identity or equality ?’ * 1000

b = ‘Which one is best? identity or equality ?’ * 1000

for i in range(n):

if a == b :

pass start = time.perf_counter()

equality_operator(10000000)

end = time.perf_counter()

print('Total time :',end - start) -------------------------------------

Total time 14.075116038999113

is operator execution time :

import sys def identity_operator(n):

a = sys.intern(‘Which one is best? identity or equality ?’ * 1000)

b = sys.intern(‘Which one is best? identity or equality ?’ * 1000)

for i in range(n):

if a is b :

pass

# print(True) start = time.perf_counter()

identity_operator(10000000)

end = time.perf_counter()

print(‘Total time :’,end — start) -----------------------------------------

Total time : 0.3351166579996061

The above results show ‘is’ beats ‘==’ operator in execution time.

Thank you for reading!

Any feedback and comments are, greatly appreciated!