2016-08-23 43 views
0

嗨,我有一個簡單的問題,但我無法找到我正在尋找的直接比較。屬性調用速度VS方法調用Python

我的問題是:

是否調用屬性往往比調用Python中的方法更快。

我有一個遊戲,我想檢查一個玩家是否已經發現了一顆行星。我可以做一個電話:

def check_exploration(self, planet): 
    return True if self.name in planet.explored_by else False 

或檢查屬性:

self.game.player.logbook[planet.name].is_discovered == True 

我問的原因是因爲我想刪除的做這件事的兩種方法之一,但我我不確定要走哪條路。

正如你可能已經注意到,當使用屬性時,我有很多電話,這是因爲我的遊戲設計。每個對象都鏈接回我的遊戲對象。所以我可以通過訪問遊戲對象並「回退」到目標對象的位置來訪問每個對象。這是乏味的,但我發現它比在模塊之間跳轉導致無盡的循環引用混亂少得多。

感謝您的建議。

+0

'返回True如果self.name在planet.explored_by其他FALSE'應該只是'在planet.explored_by' – wim

+1

返回self.name對於你的使用情況,無論哪種方式將是足夠快。出於性能原因,選擇其中之一是沒有意義的。選擇任何風格使代碼看起來最好。 – wim

+0

換句話說,[過早優化是萬惡之源](http://c2.com/cgi/wiki?PrematureOptimization) – Barmar

回答

1

我已經制作了一些腳本,生成行星在星系中的'planetsCount',並檢查'​​testsCount'時間。 (在消息的結束代碼) 一些結果:

Preparing was done in 0.0 seconds, planetsCount is 10, testsCount is 1000000 
First test time is 2.50099992752 sec 
Second test time is 2.5 sec 

Preparing was done in 0.0160000324249 seconds, planetsCount is 1000, testsCount is 1000000 
First test time is 6.97200012207 sec 
Second test time is 2.54799985886 sec 

Preparing was done in 0.406000137329 seconds, planetsCount is 100000, testsCount is 10000 
First test time is 6.09399986267 sec 
Second test time is 0.0310001373291 sec 

經由屬性檢查具有穩定的時間。 通過「列表中的項目」在小列表中檢查速度更快,但在大列表中速度很慢。 代碼低於

import random 
import base64 
import time 

class Planet(object): 
    def __init__(self, state, name): 
     self.is_discovered = state 
     self.name = base64.b64encode(name) 

class Galaxy(object): 
    planets = {} 
    explored = [] 

    def __init__(self, planetCount): 
     for planetIndex in xrange(planetCount): 
      planetName = base64.b64encode(str(planetIndex)) 
      is_discovered = random.choice([True, False]) 
      planet = Planet(is_discovered, planetName) 
      self.planets.update({planetName: planet}) 
      if is_discovered: 
       self.explored.append(planetName) 

startTime = time.time() 
planetsCount = 10 
testsCount = 1000000 
galaxy = Galaxy(planetsCount) 

print "Preparing was done in {} seconds, planetsCount is {}, testsCount is {}".format(time.time() - startTime, planetsCount, testsCount) 

startTime = time.time() 
for x in xrange(testsCount): 
    planetName = base64.b64encode(str(random.randint(0, planetsCount - 1))) 
    planetName in galaxy.explored 
print "First test time is {} sec".format(time.time() - startTime) 

startTime = time.time() 
for x in xrange(testsCount): 
    planetName = base64.b64encode(str(random.randint(0, planetsCount - 1))) 
    galaxy.planets[planetName].is_discovered 
print "Second test time is {} sec".format(time.time() - startTime) 
+0

謝謝。這很有用。因爲我不希望擊中1000個星球標誌。 – Sorade