Re: LRU cache?



#simple, but effective (sometimes)

class ICORCache:
def __init__(self,agetfunc,amaxlen=100):
self.GetValue=agetfunc
self.MaxLen=amaxlen
self.VDict={}
self.KDict={}
self.VPos=1
self.AccessRatio=0
self.HitRatio=0
def __getitem__(self,key):
self.AccessRatio=self.AccessRatio+1
id=self.KDict.get(key,0)
if id:
self.HitRatio=self.HitRatio+1
return self.VDict[id][1]
else:
v=self.VDict.get(self.VPos,0)
if v:
del self.KDict[v[0]]
ret=apply(self.GetValue,(key,))
self.VDict[self.VPos]=[key,ret]
self.KDict[key]=self.VPos
self.VPos=self.VPos+1
if self.VPos>self.MaxLen:
self.VPos=1
return ret
def dump(self):
print 'Access ratio:',self.AccessRatio
print 'Hit ratio:',self.HitRatio
print 'Hit %:',100.0*self.HitRatio/self.AccessRatio
print 'VDict len:',len(self.VDict.keys())
print 'KDict len:',len(self.KDict.keys())

#tests
if __name__=='__main__':
import random

def GetCalculatedValue(key):
x=`key`+'aaaa'
y=key*0.5
z=x+`y`
return z

# model of distribution
amax=1000
def GetIterative(key):
return key
def GetRandom(key):
return random.randint(1,amax)

l1,l2,l3=random.randint(1,amax),random.randint(1,amax),random.randint(1,amax)
def GetNDist(key):
global l1,l2,l3
l1,l2,l3=l2,l3,random.randint(1,amax)
return int((l1+l2+l3)/3)

def Main():
for aname,afunc in [['Iterative',GetIterative],
['Random',GetRandom],['NDist',GetNDist]]:
acache=ICORCache(GetCalculatedValue,100)
for i in range(1000):
k=afunc(i)
x=acache[k]
y=GetCalculatedValue(k)
if x!=y:
print 'Error!!!',k,x,y
print 'Name:',aname
acache.dump()
print
return

Main()

.



Relevant Pages

  • Re: Stats for pre kara feral druid, holy priest...
    ... Hit the armour? ... Armour and health look ok to me to start with. ... don't have to waste gem spots on def, rather use them for stam until you ... I've tended to go for green gems +stam and +def at moment, ...
    (alt.games.warcraft)
  • Re: Stats for pre kara feral druid, holy priest...
    ... Hit the armour? ... Armour and health look ok to me to start with. ... don't have to waste gem spots on def, rather use them for stam until you ... Deep and put 2 x +12 stam gems. ...
    (alt.games.warcraft)
  • Re: WPT - early assesment - hit or miss?
    ... WPT is def in the miss range.....ive only played it a handfull of times ... John in NC wrote: ... Hit or Miss? ... WPT - ??? ...
    (rec.games.pinball)
  • Re: Stats for pre kara feral druid, holy priest...
    ... Armour and health look ok to me to start with. ... So then you don't have to waste gem spots on def, rather use them for stam until you hit a point you'r comfortable with being able to soak several large hits, then start working on agi for dodge and crit. ...
    (alt.games.warcraft)
  • Re: [ANN] Nitro/Og 0.31.0
    ... Let me present another more elegant example of Nitro: ... attr_accessor:title,:body, String ... def initialize ... hit http://localhost:9999/categories/1 to view the posts in category 1. ...
    (comp.lang.ruby)