So just a simple Python list,
lst = []
for i in range(1000):
lst.append(i)
# do they have the same time cost?
a = lst[100]
b = lst[-100]
So just a simple Python list,
lst = []
for i in range(1000):
lst.append(i)
# do they have the same time cost?
a = lst[100]
b = lst[-100]
Seems to be almost equivalent
import timeit
lst = []
for i in range(1000):
lst.append(i)
# do they have the same time cost?
def a():
return lst[100]
def b():
return lst[-100]
if __name__ == '__main__':
print(timeit.timeit("scr.a()", "import scr", number=100000))
print(timeit.timeit("scr.b()", "import scr", number=100000))
>> 0.015244059000000004
>> 0.015558184000000003
Or with more iterations
>> 1.140082896
>> 1.1147582200000001
Which I would take to mean that they are as efficient as each other.