优化类模拟代码?

时间:2012-12-27 01:41:02

标签: python class random simulation

我创建了一个课程,可以从52张牌中随机有效地处理一张扑克牌。然后我写了几行来模拟52个绘图的100K模拟,因为我想知道分发是否正确实现。当我这样做时,我意识到运行SIM卡需要87秒。这对我来说似乎很长一段时间。任何人都能指出#2中可能会让它变得如此缓慢的一些事情吗?

import time
import random as rand
import numpy as np
class PlayingCard:
    ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
    suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
    def __init__(self, rank = None, suit = None):
        if rank is None: self.rank = PlayingCard.ranks[rand.randint(0,12)]
        elif rank in PlayingCard.ranks: self.rank = rank
        else: raise NameError('Invalid rank')

        if suit is None: self.suit = PlayingCard.suits[rand.randint(0,3)]
        elif suit in PlayingCard.suits: self.suit = suit
        else: raise NameError('Invalid suit')

    def identity(self):
        return (self.rank,self.suit)
#2
start = time.clock()

deck = zip(PlayingCard.ranks*4,PlayingCard.suits*13)
mat = [[PlayingCard().identity() for x in range(52)] for y in range(100000)]
res = [[(y.count(x)/52.0) for x in deck] for y in mat]
mean = [np.mean([res[y][x] for y in range(len(res))]) for x in range(52)]

end = time.clock() - start
print end

1 个答案:

答案 0 :(得分:4)

为什么不算抽奖次数?它的速度更快:

import pprint
import random

from collections import defaultdict

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']

deck = zip(ranks * 4, suits * 13)

def test(trials=100000):
    draws = defaultdict(int)

    for i in range(trials):
        draws[random.choice(deck)] += 1

    return {card: float(value) / trials for card, value in draws.iteritems()}


pprint.pprint(test(100000))

这对我来说大约0.6秒:

{('10', 'Clubs'): 0.01961,
 ('10', 'Diamonds'): 0.01897,
 ('10', 'Hearts'): 0.0196,
 ('10', 'Spades'): 0.01902,
 ('2', 'Clubs'): 0.01953,
 ('2', 'Diamonds'): 0.0201,
 ('2', 'Hearts'): 0.01889,
 ('2', 'Spades'): 0.01891,
 ('3', 'Clubs'): 0.01943,
 ('3', 'Diamonds'): 0.0198,
 ('3', 'Hearts'): 0.01893,
 ('3', 'Spades'): 0.01953,
 ('4', 'Clubs'): 0.01973,
 ('4', 'Diamonds'): 0.01946,
 ('4', 'Hearts'): 0.01822,
 ('4', 'Spades'): 0.01931,
 ('5', 'Clubs'): 0.01845,
 ('5', 'Diamonds'): 0.01956,
 ('5', 'Hearts'): 0.01978,
 ('5', 'Spades'): 0.01943,
 ('6', 'Clubs'): 0.01852,
 ('6', 'Diamonds'): 0.01903,
 ('6', 'Hearts'): 0.01928,
 ('6', 'Spades'): 0.01848,
 ('7', 'Clubs'): 0.0195,
 ('7', 'Diamonds'): 0.01881,
 ('7', 'Hearts'): 0.0194,
 ('7', 'Spades'): 0.01926,
 ('8', 'Clubs'): 0.01946,
 ('8', 'Diamonds'): 0.0188,
 ('8', 'Hearts'): 0.01985,
 ('8', 'Spades'): 0.01875,
 ('9', 'Clubs'): 0.01914,
 ('9', 'Diamonds'): 0.01908,
 ('9', 'Hearts'): 0.01937,
 ('9', 'Spades'): 0.01838,
 ('A', 'Clubs'): 0.01935,
 ('A', 'Diamonds'): 0.01843,
 ('A', 'Hearts'): 0.01957,
 ('A', 'Spades'): 0.01852,
 ('J', 'Clubs'): 0.01992,
 ('J', 'Diamonds'): 0.01933,
 ('J', 'Hearts'): 0.01881,
 ('J', 'Spades'): 0.01946,
 ('K', 'Clubs'): 0.01932,
 ('K', 'Diamonds'): 0.01845,
 ('K', 'Hearts'): 0.01935,
 ('K', 'Spades'): 0.02015,
 ('Q', 'Clubs'): 0.0189,
 ('Q', 'Diamonds'): 0.01942,
 ('Q', 'Hearts'): 0.01977,
 ('Q', 'Spades'): 0.01988}