@@ -238,41 +238,36 @@ If you reduce the value of `repeats` it will run faster, how does changing the n
238238import random
239239from timeit import timeit
240240
241- def generateInputs ( N = 25000 ):
242- random.seed(12 ) # Ensure every list is the same
243- return [random.randint(0 ,int (N/ 2 )) for i in range (N)]
244-
241+ N = 25000 # Number of elements in the list
242+ random.seed(12 ) # Ensure every list is the same
243+ data = [random.randint(0 , int (N/ 2 )) for i in range (N)]
244+
245245def uniqueSet ():
246- ls_in = generateInputs()
247- set_out = set (ls_in)
246+ set_out = set (data)
248247
249248def uniqueSetAdd ():
250- ls_in = generateInputs()
251249 set_out = set ()
252- for i in ls_in :
250+ for i in data :
253251 set_out.add(i)
254252
255253def uniqueList ():
256- ls_in = generateInputs()
257254 ls_out = []
258- for i in ls_in :
255+ for i in data :
259256 if not i in ls_out:
260257 ls_out.append(i)
261258
262259def uniqueListSort ():
263- ls_in = generateInputs()
264- ls_in.sort()
260+ ls_in = sorted (data)
265261 ls_out = [ls_in[0 ]]
266262 for i in ls_in:
267263 if ls_out[- 1 ] != i:
268264 ls_out.append(i)
269-
265+
270266repeats = 1000
271- gen_time = timeit(generateInputs, number = repeats)
272- print (f " uniqueSet: { timeit(uniqueSet, number = repeats)- gen_time:.2f } ms " )
273- print (f " uniqueSetAdd: { timeit(uniqueSetAdd, number = repeats)- gen_time:.2f } ms " )
274- print (f " uniqueList: { timeit(uniqueList, number = repeats)- gen_time:.2f } ms " )
275- print (f " uniqueListSort: { timeit(uniqueListSort, number = repeats)- gen_time:.2f } ms " )
267+ print (f " uniqueSet: { timeit(uniqueSet, number = repeats):.2f } ms " )
268+ print (f " uniqueSetAdd: { timeit(uniqueSetAdd, number = repeats):.2f } ms " )
269+ print (f " uniqueList: { timeit(uniqueList, number = repeats):.2f } ms " )
270+ print (f " uniqueListSort: { timeit(uniqueListSort, number = repeats):.2f } ms " )
276271```
277272
278273:::::::::::::::::::::::: hint
@@ -325,41 +320,34 @@ from bisect import bisect_left
325320N = 25000 # Number of elements in list
326321M = 2 # N*M == Range over which the elements span
327322
328- def generateInputs ():
329- random.seed(12 ) # Ensure every list is the same
330- st = set ([random.randint(0 , int (N* M)) for i in range (N)])
331- ls = list (st)
332- ls.sort() # Sort required for binary
333- return st, ls # Return both set and list
323+ random.seed(12 ) # Ensure every list is the same
324+ st = set ([random.randint(0 , int (N* M)) for i in range (N)])
325+ ls = list (st)
326+ ls.sort() # Sort required for binary search
334327
335328def search_set ():
336- st, _ = generateInputs()
337329 j = 0
338330 for i in range (0 , int (N* M), M):
339331 if i in st:
340332 j += 1
341333
342334def linear_search_list ():
343- _, ls = generateInputs()
344335 j = 0
345336 for i in range (0 , int (N* M), M):
346337 if i in ls:
347338 j += 1
348339
349340def binary_search_list ():
350- _, ls = generateInputs()
351341 j = 0
352342 for i in range (0 , int (N* M), M):
353343 k = bisect_left(ls, i)
354344 if k != len (ls) and ls[k] == i:
355345 j += 1
356346
357-
358347repeats = 1000
359- gen_time = timeit(generateInputs, number = repeats)
360- print (f " search_set: { timeit(search_set, number = repeats)- gen_time:.2f } ms " )
361- print (f " linear_search_list: { timeit(linear_search_list, number = repeats)- gen_time:.2f } ms " )
362- print (f " binary_search_list: { timeit(binary_search_list, number = repeats)- gen_time:.2f } ms " )
348+ print (f " search_set: { timeit(search_set, number = repeats):.2f } ms " )
349+ print (f " linear_search_list: { timeit(linear_search_list, number = repeats):.2f } ms " )
350+ print (f " binary_search_list: { timeit(binary_search_list, number = repeats):.2f } ms " )
363351```
364352
365353Searching the set is fastest performing 25,000 searches in 0.04ms.
0 commit comments