Which one is faster operation, How to find complexity?
1:
newList = list(set(itemList)) # what is the time complexity?
2:
for i in itemList: # O(n)
if i not in tempList:
tempList.append(i)
Also, I would like to know where I can find detail info of internal working on the derived data structure of python. I am sure that there must be some internal traversal, searching or hashing concept.
I tried this:
import time
itemList = [1,2,3,1,2,3,1,2,3,1,1]*1000000
def removeDups(itemList):
newList = list(set(itemList)) # what is the time complexity?
return newList
def removeDups2(itemList):
newList = []
for i in itemList: # O(n)
if i not in newList:
newList.append(i)
return newList
start_time = time.time()
removeDups(itemList)
print("--- %s seconds ---" % (time.time() - start_time))
start_time = time.time()
removeDups2(itemList)
print("--- %s seconds ---" % (time.time() - start_time))
and found that:
removeDups took --- 0.07812380790710449 seconds
removeDups2 took --- 0.23437118530273438 seconds
No Doubt, Set() is faster but how?