I'm currently doing Problem Solving with Algorithms and Data Structures
So here I'm on programming exercise on 3rd question as it says Devise an experiment that compares the performance of the del operator on lists and dictionaries.
Here is the solution from my side:
3) Devise an experiment that compares the performance of the
del
operator on lists and dictionaries.
1) lists:
def listitems(lis):
del(lis[16])
return lis
def time_taken(n):
start_time = time.time()
result = n
end_time = time.time()
return end_time - start_time, result
print time_taken(listitems([1,2,3,5,4,5,6,5,4,8,5,2,4,5,2,6,3,5,12,4,2,5,2,4,[1,5,4,5,4,54,5,5,6,4,5,[2,4,5,5,1,2,5]]]))
2) Dictionary
def dictionary(dic):
del(dic["Topic"])
return dic
def time_taken(n):
start_time = time.time()
result = n
end_time = time.time()
return end_time - start_time, result
print time_taken(dictionary({"Name": "Pradeep", "Age": 23, "Learning": "Python", "Topic": "Big O Notation"}))
Output:
(0.0, [1, 2, 3, 5, 4, 5, 6, 5, 4, 8, 5, 2, 4, 5, 2, 6, 5, 12, 4, 2, 5, 2, 4, [1, 5, 4, 5, 4, 54, 5, 5, 6, 4, 5, [2, 4, 5, 5, 1, 2, 5]]])
(0.0, {'Age': 23, 'Name': 'Pradeep', 'Learning': 'Python'})
So according to Big O notation list output time should be different from dictionary output time
Reference: Time Complexity