0

I was benchmarking the time it takes three seemingly similar algorithms to finish running in Python. They are all trying to find the largest element in an unsorted list. Here's the code:

from time import time
from random import random

LIST_LEN = 1000
my_list = [int(random() * LIST_LEN) for i in range(LIST_LEN)]

# Iterate with an index variable
start_time = time()
my_max = 0
i = 0
while i < len(my_list):
    if my_list[i] > my_max: 
        my_max = my_list[i]
    i += 1
my_time = time() - start_time
print(' Indexed:\t', my_time)

# Iterate using iterator pattern
start_time = time()
my_max = 0
for num in my_list:
    if num > my_max: 
        my_max = num
my_time = time() - start_time
print('Iterator:\t', my_time)

# Use built-in max() function
start_time = time()
my_max = max(my_list)
max_time = time() - start_time
print('Built-in:\t', max_time)

And here are the results:

 Indexed:        0.0002579689025878906
Iterator:        6.604194641113281e-05
Built-in:        1.7404556274414062e-05

I get similar results every time I run the program, whether or not I'm running locally or on a server. What's up with the notable difference in the three methods? Accessing using indices seems to be about 4 times slower than the iterator pattern, which seems to be about 4 times slower than Python's built-in max() function.

Rapid Readers
  • 303
  • 1
  • 3
  • 13
  • 5
    The built-in `max` function is likely written in C, which will naturally be faster than any equivalent Python implementation. – 0x5453 Aug 09 '22 at 21:29
  • 1
    See answers at a question like [this one](https://stackoverflow.com/questions/24578896/python-built-in-sum-function-vs-for-loop-performance). – duckboycool Aug 09 '22 at 21:33
  • 3
    CPython is an interpreter and it optimize nearly nothing. Accessing `my_list[i]` twice is twice slower than accessing `my_list[i]` once for example. This is not the case in compiled C/C++ code for example (or any low-level compiled language). If you use PyPy (another implementation of Python), then you will get different results since PyPy is able to optimize a bit more such a thing but it is far from being as good as a C/C++ code. – Jérôme Richard Aug 09 '22 at 22:24

1 Answers1

0

Your first method uses a while loop which needs to do a comparison i < len(my_list), perform the function len(my_list) every while loop, and increase i.

The second method has fewer python operations, therefore faster.

The third method is using built-in C function.

They are all O(N) time complexity.

Tom McLean
  • 5,583
  • 1
  • 11
  • 36