Python has an awesome feature called timeit that allows you to test how long a snippet of code took to run. In this example were going to test another cool feature of Python, negative indexing.

Negative indexing allows us to get an array value using a negative number as the index.

array = [1, 2, 3, 4, 5, 6, 7, 8]
print array[-1]

Output: 8
Using negative indexing we can see the output would be 8 like we expected.

So let's do a test:

import timeit

array = [1, 2, 3, 4, 5, 6, 7, 8]  
test1 = timeit.Timer(stmt="array[len(array) - 2]", setup="from __main__ import array")  
#setup needed to use variables like the array
test2 = timeit.Timer(stmt="array[-2]", setup="from __main__ import array")

print test1.timeit() # array[len(array) - 2]  
print test2.timeit() # array[-2]  

Output:
0.189550161362
0.0410318374634

You'll get slightly different answers every time, but the obvious winner here is negative indexing. Now you know how to time code snippets and use negative indexing!