def insert_test(repetitions): """Insert REPETITIONS elements into the 0 index of both lists. """ pyList, linkedList = [], Link.empty print("Inserting {} items into the start of a Python list".format( repetitions), end=": ") start = time() for _ in range(repetitions): pyList.insert(0, 0) end = time() pyTime = end - start print("took " + format(pyTime, '.6f') + 's') print("Inserting {} items into the head of a linked list".format( repetitions), end=": ") start = time() for _ in range(repetitions): linkedList = Link(0, linkedList) end = time() linkTime = end - start print("took " + format(linkTime, '.6f') + 's') print("Ratio of Python List to Linked List: " + format(pyTime / linkTime, '.6f'))
def range_link(start, end): """Returns a Link that contains the values from START upto END. """ linked = Link.empty current = end while current >= start: linked = Link(current, linked) current = current - 1 return linked