Reputation: 937
Hi I have a simple question but I wasn't able to find the direct comparison I was looking for.
My question is:
Does calling attribute tend to be faster than calling a method in Python.
I have a game and I want to check whether or not a player has discovered a planet. I can either do a call to:
def check_exploration(self, planet):
return True if self.name in planet.explored_by else False
or check the attribute:
self.game.player.logbook[planet.name].is_discovered == True
The reason I'm asking is because I want to remove one of the two ways of doing it but I'm not sure which way to go.
As you might have notice I have a lot of calls when using the attribute, this is because of my game design. Every object links back to my game object. So I can access each object by going to the game object and "back down" into the location of the target object. It is tedious but I found that it is much less messy than jumping around between modules which causes endless circular references.
thank you for your advice.
Upvotes: 0
Views: 102
Reputation: 56
I've maked some script that generate 'planetsCount' of planets in galaxy, and checks 'testsCount' times. (code at end of message) Some results:
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
Checking via attribute have stable time. Checking via "item in list" faster on small lists, but very slow on big lists. Code was below
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)
Upvotes: 1