Created
June 4, 2017 23:10
-
-
Save rustybrooks/2b583a6d3bfff40182e4f1648f542b24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
import random | |
import timeit | |
max_cards = 30 | |
simulations = 10000 | |
print('simulations = {}'.format(simulations)) | |
dict_deck = {i: 1 for i in range(52)} | |
list_deck = range(52) | |
def method1(num_to_deal): | |
dealt_cards = [] | |
dealt_cards.append(random.randint(0,51)) | |
for i in range(num_to_deal-1): | |
card = random.randint(0,51) | |
while card in dealt_cards: | |
card = random.randint(0,51) | |
dealt_cards.append(card) | |
return dealt_cards | |
def method1a(num_to_deal): | |
dealt_cards = {} | |
for i in range(num_to_deal): | |
card = random.randint(0, 51) | |
while card in dealt_cards: | |
card = random.randint(0,51) | |
dealt_cards[card] = 1 | |
return dealt_cards.keys() | |
def method2(num_to_deal): | |
deck = copy.copy(list_deck) | |
dealt_cards = [] | |
for i in range(num_to_deal): | |
card = random.randint(0,len(deck)-1) | |
dealt_cards.append(deck[card]) | |
deck.remove(deck[card]) | |
return dealt_cards | |
def method2a(num_to_deal): | |
deck = copy.copy(list_deck) | |
return map(lambda x: deck.pop(random.randint(0,len(deck)-1)), range(num_to_deal)) | |
def method3(num_to_deal): | |
deck = dict_deck.copy() | |
return map(lambda x: deck.pop(random.choice(deck.keys())), range(num_to_deal)) | |
def method4(num_to_deal): | |
return random.sample(range(52), num_to_deal) | |
for num_cards in range(1, max_cards, 10): | |
print("------------------------------------------------ {}".format(num_cards)) | |
for fn, label in ( | |
(method1, 'Collision'), | |
(method1a, 'Dict Collision'), | |
(method2, 'List reduction'), | |
(method2a, 'Simplified List reduction'), | |
(method3, 'dict reduction'), | |
(method4, 'random sample'), | |
): | |
elapsed_time = min(timeit.repeat(lambda: fn(num_cards), number=simulations, repeat=5)) | |
print('%40s: %0.1fk/s' % (label, (1/(elapsed_time/simulations)/1000.0))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment