私は、本来あるべきよりもはるかに遅い Python コードをいくつか持っています。
#Generate planets
for t in range(stars*3): #There are 3 planets for every star, but not every star will have 3 planets
theplanet=Planet()
if random.randint(0,100) <= 25: #25% of planets have life
theplanet.tl=random.randint(1,9) #With a random tech level
else:
theplanet.tl=0 #The rest don't
theplanet.star=stardict[random.choice(list(stardict.keys()))] #Choose a random star
theplanet.star.planets+=(theplanet,) #Append the new planet to the star's list of planets
theplanet.name=theplanet.star.name+"-"+str(len(theplanet.star.planets)) #Name the planet Starname-X, where X is the length of the star's planets tuple. Since this increases every time a planet is added, it will be 1 for the first planet, 2 for the next, etc...
if math.floor((t/(stars*3))*100)==(t/(stars*3))*100: print("Generating planets: "+str((t/(stars*3))*100)+"% done.")
star=stardict[random.choice(list(
ボトルネックはetc... 行にあると確信しています。私はここで推測していますが、辞書内のすべてのエントリを検索し、どのエントリが正しいキーを持っているかを確認することで、辞書が機能すると想定しています。繰り返しになりますが、リストはエントリ番号から派生したメモリ位置で情報を読み取るだけであり、非常に大きな (正確には 200,000 エントリ) リスト/ディクテーションははるかに高速です。
dict のエントリをリストに変換すると、このコードが高速になりますか? これを行うにはどうすればよいですか(ドキュメントを今見直して、そのための機能を見たと思いました...)?これをより速くするために誰かが気付く他の方法はありますか?