$25
Experimentally determine the running time of list and dictionary operations in Python by writing code to test how long they take to run on various sized lists and dictionaries. Write a report explaining what you found, and draw whatever conclusions you can about how lists and dictionaries might be structured in order to give this behavior. You will probably find it useful to write a function that produces a .csv file of data about the behavior of a given function and then to use Excel to plot the data and find a curve that fits it.
.cvs stands for “comma-separated values.” A .cvs file is a text file that can be read by a spreadsheet program, in which values go into a new cell in the same line when it reaches a comma, and to the next row when it reaches a new line (“\r\n”). To start writing a text file in python, use the command:
f_out = open(filename,"w+")
You can a string to the file with the command f_out.write(string)
and when you are done, close the file using the command f_out.close()
Also note that you can import the time module and use the command time.time() to get the current system time. By using subtraction, you can use this to measure how long it took to execute a given command.
At a minimum, you should determine the running time of inserting and deleting elements from the beginning, middle, and end of different length lists, and of determining if an element is in a list; and likewise, adding and deleting items to and from a dictionary, and determining if an element is in the dictionary. Note that, unlike lists, dictionaries aren’t ordered, so it doesn’t make sense to think of putting something at the beginning, middle, or end of a dictionary.