मुझे पता है कि यह पोस्ट पुराना है, लेकिन मैंने हाल ही में थोड़ा परीक्षण किया है।
'''
Independent Study, Timing insertion list method in python
'''
import time
def make_list_of_size(n):
ret_list = []
for i in range(n):
ret_list.append(n)
return ret_list
#Estimate overhead of timing loop
def get_overhead(niters):
'''
Returns the time it takes to iterate a for loop niter times
'''
tic = time.time()
for i in range(niters):
pass #Just blindly iterate, niter times
toc = time.time()
overhead = toc-tic
return overhead
def tictoc_midpoint_insertion(list_size_initial, list_size_final, niters,file):
overhead = get_overhead(niters)
list_size = list_size_initial
#insertion_pt = list_size//2 #<------- INSERTION POINT ASSIGMNET LOCATION 1
#insertion_pt = 0 #<--------------- INSERTION POINT ASSIGNMENT LOCATION 4 (insert at beginning)
delta = 100
while list_size <= list_size_final:
#insertion_pt = list_size//2 #<----------- INSERTION POINT ASSIGNMENT LOCATION 2
x = make_list_of_size(list_size)
tic = time.time()
for i in range(niters):
insertion_pt = len(x)//2 #<------------- INSERTION POINT ASSIGNMENT LOCATION 3
#insertion_pt = len(x) #<------------- INSERTION POINT ASSIGN LOC 5 insert at true end
x.insert(insertion_pt,0)
toc = time.time()
cost_per_iter = (toc-tic)/niters #overall time cost per iteration
cost_per_iter_no_overhead = (toc - tic - overhead)/niters #the fraction of time/iteration, #without overhead cost of pure iteration
print("list size = {:d}, cost without overhead = {:f} sec/iter".format(list_size,cost_per_iter_no_overhead))
file.write(str(list_size)+','+str(cost_per_iter_no_overhead)+'\n')
if list_size >= 10*delta:
delta *= 10
list_size += delta
def main():
fname = input()
file = open(fname,'w')
niters = 10000
tictoc_midpoint_insertion(100,10000000,niters,file)
file.close()
main()
5 पदों जहां प्रविष्टि किया जा सकता है देखें: list.insert की जटिलता() हे (एन) होने के लिए
कोड प्रकट होता है। लागत निश्चित रूप से की एक समारोह है कितनी बड़ी सूची है, और कितने करीब आप सूची की शुरुआत करने के लिए कर रहे हैं (यानी कितने स्मृति स्थल के पुनर्गठन किया जाना है)
साजिश के बाईं छवि पर ध्यान न दें
स्रोत
2017-03-23 04:57:16
धन्यवाद रायग्यू-इसकी सराहना करते हैं। Kaizer.se द्वारा संदर्भित टाइम कॉम्प्लेक्सिटी डॉक से भी: एक्स इन ........... ओ (एन) मिनट (ओं), अधिकतम (x) ... ओ (एन) – Dan
@ ryeguy: यह देखने के लिए आश्चर्यचकित है कि ओ (एन) संचालन डालें और हटाएं। ओ (1) को सम्मिलित करने और हटाने की समय जटिलता को कम करने के लिए सूची डेटा संरचना का पूरा बिंदु नहीं है? उपरोक्त से, ऐसा लगता है कि अंतर्निहित डेटा संरचना एक सरणी है। क्या मैं कुछ भूल रहा हूँ? –
@AKE देखें [सरणी सूची] (http://en.wikipedia.org/wiki/Array_list)। डेटा संरचनाओं के विभिन्न कार्यान्वयन में ट्रेडऑफ हैं। आपके ठेठ ओ (1) में सम्मिलित/हटाएं सूची में अक्सर आपको आइटम (ओ) के रूप में प्राप्त होता है। –