[英]Increase the speed of my code
我創建了下面的代碼,它使用一系列值,並在x和r之間生成10個均值為8000的數字
為了盡可能滿足覆蓋范圍的規范,我還計算了標准差,這是擴展的好方法。 因此,只要樣本集滿足8000的均值標准,我就將其與之前的匹配進行比較,並不斷選擇具有最高標准差(均值= 8000)的樣本。
def node_timing(average_block_response_computational_time, min_block_response_computational_time, max_block_response_computational_time):
sample_count = 10
num_of_trials = 1
# print average_block_response_computational_time
# print min_block_response_computational_time
# print max_block_response_computational_time
target_sum = sample_count * average_block_response_computational_time
samples_list = []
curr_stdev_max = 0
for trials in range(num_of_trials):
samples = [0] * sample_count
while sum(samples) != target_sum:
samples = [rd.randint(min_block_response_computational_time, max_block_response_computational_time) for trial in range(sample_count)]
# print ("Mean: ", st.mean(samples), "Std Dev: ", st.stdev(samples), )
# print (samples, "\n")
if st.stdev(samples) > curr_stdev_max:
curr_stdev_max = st.stdev(samples)
samples_best = samples[:]
return samples_best[0]
我將列表中的第一個值用作時序值,但是此代碼確實很慢,我需要在仿真過程中多次調用此代碼數千次,因此需要提高代碼效率
任何人有任何建議如何?
為了了解我們將在哪些方面獲得最快的速度改進,我首先分析了您的代碼。
import cProfile
pr = cProfile.Profile()
pr.enable()
for i in range(100):
print(node_timing(8000, 7000, 9000))
pr.disable()
pr.print_stats(sort='time')
結果的頂部顯示了您的代碼大部分時間都在哪里:
23561178 function calls (23561176 primitive calls) in 10.612 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
4502300 3.694 0.000 7.258 0.000 random.py:172(randrange)
4502300 2.579 0.000 3.563 0.000 random.py:222(_randbelow)
4502300 1.533 0.000 8.791 0.000 random.py:216(randint)
450230 1.175 0.000 9.966 0.000 counter.py:19(<listcomp>)
4608421 0.690 0.000 0.690 0.000 {method 'getrandbits' of '_random.Random' objects}
100 0.453 0.005 10.596 0.106 counter.py:5(node_timing)
4502300 0.294 0.000 0.294 0.000 {method 'bit_length' of 'int' objects}
450930 0.141 0.000 0.150 0.000 {built-in method builtins.sum}
100 0.016 0.000 0.016 0.000 {built-in method builtins.print}
600 0.007 0.000 0.025 0.000 statistics.py:105(_sum)
2200 0.005 0.000 0.006 0.000 fractions.py:84(__new__)
...
從此輸出中,我們可以看到我們花費了大約7.5秒(從10.6秒中)生成隨機數。 因此,使此速度明顯更快的唯一方法是生成更少的隨機數或更快地生成它們。 您沒有使用密碼隨機數生成器,所以我沒有辦法更快地生成數字。 但是,我們可以對算法稍加改動,並大大減少我們需要生成的值的數量。
如果不接受均值恰好為8000的樣本,那么如果我們接受均值為8000±0.1%的樣本(那我們將取均值為7992至8008的樣本)怎么辦? 由於有點不精確,我們可以大大加快算法的速度。 我將while
條件替換為:
while abs(sum(samples) - target_sum) > epsilon
其中epsilon = target_sum * 0.001
。 然后,我再次運行了腳本,並獲得了更好的探查器編號。
232439 function calls (232437 primitive calls) in 0.163 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
100 0.032 0.000 0.032 0.000 {built-in method builtins.print}
31550 0.026 0.000 0.053 0.000 random.py:172(randrange)
31550 0.019 0.000 0.027 0.000 random.py:222(_randbelow)
31550 0.011 0.000 0.064 0.000 random.py:216(randint)
4696 0.010 0.000 0.013 0.000 fractions.py:84(__new__)
3155 0.008 0.000 0.073 0.000 counter.py:19(<listcomp>)
600 0.008 0.000 0.039 0.000 statistics.py:105(_sum)
100 0.006 0.000 0.131 0.001 counter.py:4(node_timing)
32293 0.005 0.000 0.005 0.000 {method 'getrandbits' of '_random.Random' objects}
1848 0.004 0.000 0.009 0.000 fractions.py:401(_add)
允許平均值比目標高出0.1%, randint
的調用randint
減少100倍。 自然,代碼的運行速度也快了100倍(現在花了大部分時間在控制台上打印)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.