Advertisement
Guest User

ShuffleVsSample

a guest
Apr 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. from __future__ import print_function
  2. import timeit
  3.  
  4. # Size of list to make random selection from
  5. LIST_SIZE = 100
  6. # Number of timeit runs
  7. NUM_RUNS = 100000
  8. # Number of items to choose
  9. NUM_ITEMS = 10
  10.  
  11.  
  12. sample = '''
  13. import random
  14. maps = list(range({}))
  15. def sample(maps, n):
  16.    for i, m in enumerate(random.sample(maps,n)):
  17.        yield i, m
  18. '''.format(LIST_SIZE)
  19.  
  20.  
  21. shuffle='''
  22. import random
  23. maps = list(range({}))
  24. def shuffle(maps, n):
  25.    new = list(maps)
  26.    random.shuffle(new)
  27.    for i, m in enumerate(new[:n]):
  28.        yield i, m
  29. '''.format(LIST_SIZE)
  30.  
  31.  
  32. print('***SAMPLE***')
  33. print(timeit.timeit('list(sample(maps, {}))'.format(NUM_ITEMS), setup=sample, number=NUM_RUNS))
  34. print('***SHUFFLE**')
  35. print(timeit.timeit('list(shuffle(maps, {}))'.format(NUM_ITEMS), setup=shuffle, number=NUM_RUNS))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement