#Day19 - Comparing the performance of sorting vs sorted and reverse vs reversed

rahulbanerjee99

Rahul Banerjee

Posted on April 9, 2021

#Day19 - Comparing the performance of sorting vs sorted and reverse vs reversed

We will try to find out which is faster between sort(), sorted() and reverse(), reversed().

Sort and Sorted

Both sort and sorted are used to sort the elements in a list. There are a two differences

  • Sorted() is an in-built Python function while sort() is a method of the class list
  • Sorted() returns a list with its element sorted while sort() is an in-place method. It updates the original list.
import random

lst1 = [random.randint(0,10) for _ in range(5)]

print(lst1)
print(lst1.sort())
print(lst1)

'''
OUTPUT

[2, 9, 10, 7, 0]
None
[0, 2, 7, 9, 10]

'''
Enter fullscreen mode Exit fullscreen mode

As you can see, the second print statement prints None since sort() is an in-place method. In the third print statement, when we print lst1, we can see that the elements are sorted.

import random

lst1 = [random.randint(0,10) for _ in range(5)]

print(lst1)
print(sorted(lst1))
print(lst1)

'''
OUTPUT

[3, 6, 7, 5, 5]
[3, 5, 5, 6, 7]
[3, 6, 7, 5, 5]

'''
Enter fullscreen mode Exit fullscreen mode

Unlike sort(), the second print statement prints out the list with the elements sorted while the third print statement prints the list in its original order.

Comparing the time taken by sort and sorted

We have a list of 1000 elements and we will try to sort it using both methods.

import time
import random

lst1=[random.randint(1,1000) for i in range(1000)]

lst2 = lst1.copy()

s1 = time.perf_counter()
sorted(lst1)
print(f'It took {time.perf_counter() - s1} seconds for sorted()')

s1 = time.perf_counter()
lst2.sort()
print(f'It took {time.perf_counter() - s1} seconds for sort()')
Enter fullscreen mode Exit fullscreen mode

Below is the output

It took 0.00015853159129619598 seconds for sorted()
It took 0.0010612830519676208 seconds for sort()
Enter fullscreen mode Exit fullscreen mode

It seems sorted() is faster but let's try running it again

It took 0.00016496609896421432  seconds for sorted()
It took 0.00013628974556922913 seconds for sort()
Enter fullscreen mode Exit fullscreen mode

Hmm, it seems sort is faster by a tiny margin this time

The test seems inconclusive, let's try something else
We will run the above comparison 25 times and during each iteration, we will check which method was faster.

import time
import random

sort_count = 0
sorted_count = 0
for i in range(25):
    print(f'Iteration {i}')
    lst1=[random.randint(1,1000) for i in range(1000)]
    lst2 = lst1.copy()

    s1 = time.perf_counter()
    sorted(lst1)
    sorted_time = time.perf_counter() - s1

    s1 = time.perf_counter()
    lst2.sort()
    sort_time = time.perf_counter() - s1

    if sorted_time < sort_time:
        sorted_count += 1
    else:
        sort_count += 1
print(f'sort_count is {sort_count}')
print(f'sorted_count is {sorted_count}')
Enter fullscreen mode Exit fullscreen mode

Below is the output

sort_count is 22
sorted_count is 3
Enter fullscreen mode Exit fullscreen mode

Based on the above experiment, it seems sort is faster than sort when trying to sort the same list.

Reverse and Reversed

reverse() and reversed() are similar to sort() and sorted() respectively. reverse() is a built-in python function while reversed() is an in-place method of the list class. The only difference between sorted() and reversed() is that sorted() returns an list object while reversed() returns an list_reverseiterator object which can be typecase to a list using list()

I won't be showing the example since it's similar to the examples discussed in the above section. We will move on to the more interesting stuff which is the performance comparison.

import time
import random

reverse_count = 0
reversed_count = 0
for i in range(25):
    print(f'Iteration {i}')
    lst1=[random.randint(1,1000) for i in range(1000)]
    lst2 = lst1.copy()

    s1 = time.perf_counter()
    reversed(lst1)
    reversed_time = time.perf_counter() - s1

    s1 = time.perf_counter()
    lst2.reverse()
    reverse_time = time.perf_counter() - s1

    if reversed_time < reverse_time:
        reversed_count += 1
    else:
        reverse_count += 1
print(f'reversed_count is {reversed_count}')
print(f'reverse_count is {reverse_count}')
Enter fullscreen mode Exit fullscreen mode

Below is the output

reversed_count is 0
reverse_count is 25
Enter fullscreen mode Exit fullscreen mode

It seems like reverse() is the clear winner

Summary

  • sort() and reverse() are in-place methods of the list object and update the original list
  • sorted() returns a list with its element sorted
  • reversed() returns a list_reverseiterator object
  • Based on our above experiments sort() is faster than sorted() and reverse() is faster than reversed()
💖 💪 🙅 🚩
rahulbanerjee99
Rahul Banerjee

Posted on April 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related