0

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

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • You are not actually doing the deletion. You do `result = n` instead of `result = dictionary(n)` or `result = listitems(n)`. The place you currently have `listitems` and `dictionary` will cause them to be evaluated before `time_taken` is run. – Score_Under Apr 06 '16 at 07:26

2 Answers2

1

Below is how I did the implementation. The results show the del operation on dictionaries is faster than in lists. The code is pretty self-explanatory.

import timeit
import random

def del_dict_items(x):
# Insert the index
    random_index = random.randrange(len(x) - 1)
    try:
        del x[random_index]
    except KeyError:
        x.setdefault(random_index, None)
        del x[random_index]

print("i\t\tlist_del_time\t\tdict_del_time")
for i in range(10000, 100001, 1000):
    t_list = timeit.Timer("del x[random.randrange(len(x)-1)]", "from __main__ import random, x")
    t_dict = timeit.Timer("del_dict_items(x)", "from __main__ import random, x, del_dict_items")
    x = list(range(i))
    list_del_time = t_list.timeit(number=1000)
    x = {j:None for j in range(i)}
    dict_del_time = t_dict.timeit(number=1000)
    print("%d %10.3f %20.3f" %(i, list_del_time, dict_del_time))
sloppyhead
  • 63
  • 6
0

To check the execution time of a function it is advised to use timeit

The problem in you're method is when you call time_taken(listitems(values)) you are actually executing listitems function first then passing the value to time_taken so what time_taken does is it shows the time taken to store the given variable in result so you are getting same time

using timeit module:

timeit.timeit('lis = [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]]]; del(lis[16])', number=10000)
Out[41]: 0.0182793565268895

timeit.timeit('dic = {"Name": "Pradeep", "Age": 23, "Learning": "Python", "Topic": "Big O Notation"}; del(dic["Topic"])', number=10000)
Out[42]: 0.006386155956171535

So it is clear that del operation on dic is faster than list

Do look into this Q&A on SO regarding measuring time taken by a function

Community
  • 1
  • 1
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • Hmm it works but can you teach me step by step how this timeit works and why you have used del inside timeit and what is this variable number??? I'm confused. Please help me with this also.. –  Apr 06 '16 at 11:38
  • @PradeepSukhwani I have pointed the `timeit` official page I think the page would answer this question of yours. If you are still confused then let me know :). – The6thSense Apr 06 '16 at 13:38