Python Forum
Using range over slicing when looping through lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using range over slicing when looping through lists
#1
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!
Reply
#2
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.
GJG likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
GJG likes this post
Reply
#4
(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
GJG likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020