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.