avatarSivasai Yadav Mudugandla

Summary

The web content discusses the differences between the "is" (identity) and "==" (equality) operators in Python, advising on their appropriate usage for efficient string comparison and highlighting the performance benefits of using "is" for comparing long strings.

Abstract

The article titled "“is” and “==” in Python: Speed up your string comparisons" explains the distinct roles of the "is" and "==" operators in Python. It emphasizes the importance for Python programmers to understand when to use each operator correctly. The "is" operator checks if two variables refer to the same object in memory, while the "==" operator compares the values of two variables. The author suggests that for heavy string comparison operations, using the "is" operator can significantly improve performance, as it compares memory addresses rather than the contents of strings character by character. The article includes examples and timing tests to demonstrate that "is" is faster than "==" for string comparisons, especially when dealing with long strings. The author concludes by recommending the use of "is" for speeding up string comparisons, although the choice may vary based on individual preferences and the specific use case.

Opinions

  • The author believes that it's crucial for Pythonistas to clearly understand the difference between "is" and "==" to use them appropriately.
  • The article suggests that Python's "is" operator is more efficient for string comparisons, particularly with long strings, due to its faster execution time and lower CPU utilization.
  • The author points out that while "==" compares the values of two objects, "is" checks for object identity, which is a more efficient comparison method for heavy string operations.
  • It is noted that Python may not automatically intern all strings, but using sys.intern() can force string interning to ensure that the "is" operator can be used effectively for comparison.
  • The author's stance is that, for performance reasons, the "is" operator should be preferred over "==" when the objective is to compare strings, provided that the strings are interned.

“is” and “==” in 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

  1. What is equality and identity operators
  2. When to use equality and identity operators
  3. 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

  1. 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.
  2. 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 :’,endstart)
-----------------------------------------
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!

Python
Python Programming
Python3
Programming
Coding
Recommended from ReadMedium