python - Flyweight pattern - Memory footprint -


i'm learning python , i've thought nice excuse refresh pattern knowledge , in case, flyweight pattern.

i created 2 small programs, 1 not optimized , 1 implementing flyweight pattern. tests purposes, i'm creating army of 1'000'000 enemy objects. each enemy can of 3 types (soldier, ninja or chief) , assign motto each type.

what check that, un-optimized program, 1'000'000 enemies with, each , of them, type , "long" string containing motto. optimized code, i'd create 3 objects (enemytype) matching each type , containing 3 times motto's strings. then, add member each enemy, pointing desired enemytype.

now code (excerpts only) :

  1. un-optimized program

    in version, each enemy stores type , motto.

    enemylist = [] enemytypes = {'soldier' : 'sir, yes sir!', 'ninja' : 'always behind !', 'chief' : 'always behind ... lot of lines '} in range(1000000):   randomposx = 0 # random.choice(range(1000))   randomposy = 0 # random.choice(range(1000))   randomtypeindex = 0 # random.choice(range(0,len(enemytypes)))   enemytype = enemytypes.keys()[randomtypeindex]    # here, type , motto parameters of each enemy object.   enemylist.append(enemy(randomposx, randomposy, enemytype, enemytypes[enemytype])) 
  2. optimized program

    in version, each enemy has member of enemytype object stores type , motto. 3 instances of enemytype created , should see impact in memory footprint.

    enemylist = [] soldierenemy = enemytype('soldier', 'sir, yes sir!')          ninjaenemy = enemytype('ninja', 'always behind !') chiefenemy = enemytype('chief', 'always behind ... lot of lines.') enemytypes = {'soldier' : soldierenemy, 'ninja' : ninjaenemy, 'chief' : chiefenemy}  enemycount = {}  in range(1000000): randomposx = 0 # random.choice(range(1000)) randomposy = 0 # random.choice(range(1000)) randomtypeindex = 0 #random.choice(range(0,len(enemytypes))) enemytype = enemytypes.values()[randomtypeindex]  # here, each enemy has reference on type. enemylist.append(enemy(randomposx, randomposy, enemytype)) 

now i'm using memory footprint (at last lines before application closes itself) :

import os import psutil ... # return memory usage in mb process = psutil.process(os.getpid()) print process.get_memory_info()[0] / float(2 ** 20) 

my problem that, don't see difference between output of 2 programs :

optimized = 384.0859375 mb
un-optimized = 383.40234375 mb

is proper tool memory footprint ? i'm new python problem code checked enemytype objects in second solution , indeed have 3 occurences. therefore should have 3 motto strings instead of 1'000'000.

i've read tool called heapy python, more accurate here ?

as far tell code in question, in both cases you're using references same small number of instances anyway.

take "unoptimized" version's:

enemylist.append(enemy(randomposx, randomposy, enemytype, enemytypes[enemytype])) 

indeed enemytypes[enemytype] string, might have made think have many instances of strings. in reality, each of objects has 1 of three same string objects.


you can check comparing ids of members. make set of ids, , see if larger 3.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -