Python Forum

Full Version: Using range over slicing when looping through lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Folks! Beginner question. Which one of these two are less processor costly?
my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
largest = my_list[0]

for i in range(1, len(my_list)):
    if my_list[i] > largest:
        largest = my_list[i]

print(largest)
Or

my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
largest = my_list[0]

for i in my_list[1:]:
    if i > largest:
        largest = i

print(largest)
Or not much of a difference, except that latter looks more elegant ?
Thank you!
Test your code.

max() wins.

PS C:\Users\Andre> ipython.exe
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: def test1():
   ...:     my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
   ...:     largest = my_list[0]
   ...:
   ...:     for i in range(1, len(my_list)):
   ...:         if my_list[i] > largest:
   ...:             largest = my_list[i]
   ...:
   ...:     print(largest)
   ...:

In [2]: def test2():
   ...:     my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
   ...:     largest = my_list[0]
   ...:
   ...:     for i in my_list[1:]:
   ...:         if i > largest:
   ...:             largest = i
   ...:
   ...:     print(largest)
   ...:

In [3]: def test3():
   ...:     my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
   ...:     return max(my_list)
   ...:

In [4]: %timeit test1
14.5 ns ± 0.0177 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

In [5]: %timeit test2
15 ns ± 0.517 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

In [6]: %timeit test3
12.9 ns ± 0.161 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
Often it's not clear if something is faster or slower. Then you have to test it.
One rule is, that copying data takes time and calling functions also.
Python copies data, if you slice a list for example.
I recommend elimination the print statement and increase number to 10000 or even 100000
import timeit

# Function 1
func = """\
my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
largest = my_list[0]

for i in range(1, len(my_list)):
    if my_list[i] > largest:
        largest = my_list[i]
print(largest)
"""
# for better sanple, increase number (iterations)
et = timeit.timeit(stmt=func, number=1)
print(f"Elapsed time method 1: {et}")

# function 2
func = """\
my_list = [2, 3, 11, 5, 1, 9, 7, 15, 13]
largest = my_list[0]
 
for i in my_list[1:]:
    if i > largest:
        largest = i
 
print(largest)
"""
# for better sanple, increase number (iterations)
et = timeit.timeit(stmt=func, number=1)
print(f"Elapsed time method 2: {et}")
Output:
15 Elapsed time method 1: 3.121374174952507e-05 15 Elapsed time method 2: 9.709969162940979e-06
(Jan-04-2021, 09:36 AM)GJG Wrote: [ -> ]Or not much of a difference, except that latter looks more elegant ?
And it is the pythonic way to iterate over sequence

https://python-forum.io/Thread-Basic-Nev...n-sequence