0
我正在編寫癌細胞羣體增長的模擬,並且我使用numpy.random函數來模擬獲取或失去突變的細胞。我通過分析確定代碼中的瓶頸(大約70%的運行時間)是包含numpy.random函數的前幾行。這裏變量num_steps
是一個大數,大約一百萬:提高numpy.random函數的速度
def simulate(mu, gamma, beta, num_steps, threshold):
mutation_num = 0 # the index of the mutation (we assume each mutation only occurs once)
population = {() : 1} # represents population: tuple of mutations and number of cells with those mutations
for epoch in range(num_steps):
next_population = {}
for mutations, size in population.items():
born = np.random.binomial(size, birth_rate)
if np.random.binomial(born, gamma):
return True
mut_loss = 0 # initializing in case variable is not created
if mutations:
mut_gain, mut_loss, mut_same = np.random.multinomial(born, [mu, beta, 1-mu-beta])
else:
mut_gain, mut_same = np.random.multinomial(born, [mu, 1-mu])
.....
有沒有一種方法,使np.random.binomial
和np.random.multinomial
功能運行得更快?我嘗試過使用Cython,但這沒有幫助。
你可能想要用'xrange'替代'range',這是一個懶惰的等價物,並且看看這有多幫助;或者用一段時間替換它並手動增加值(這實際上是相同的優化)。 –
@BurhanKhalid'range'與'xrange'的觀察結果只對Python 2有效。你確定OP沒有使用Python 3嗎? –
我正在使用python 3.正如我已經提到的那樣,profiler表明瓶頸是在numpy隨機函數的周圍。 – Alex