Python Program that times nested list and flattened list














































Python Program that times nested list and flattened list



import time

# Nested, 3x2.
nested_list = []
nested_list.append([10, 20, 30])
nested_list.append([40, 50, 60])

# Flattened, 3x2.
flat_list = [10, 20, 30, 40, 50, 60]

# Test the two lists.
print(nested_list[1][2])
print(flat_list[1 + (2 * 2)])

print(time.time())

# Version 1: access element in nested list.
for i in range(0, 100000000):
    if nested_list[1][2] != 60:
        print(False)

print(time.time())

# Version 2: access element in flattened list.
for i in range(0, 100000000):
    if flat_list[1 + (2 * 2)] != 60:
        print(False)

print(time.time())
Output:
60
60
1420077374.795
1420077375.334:    Nested list, 0.539 s
1420077375.794:    Flat list,   0.460 s  [PyPy]


Comments