def update(self, otherdict): """ @return: self """ assert self._assert_invariants() if len(otherdict) >= (self.m-2): # -2 for the sentinel nodes # optimization self.clear() assert self._assert_invariants() i = otherdict.iteritems() try: while len(self.d) < self.m: (k, v,) = i.next() assert self._assert_invariants() self[k] = v assert self._assert_invariants() return self except StopIteration: _assert(False, "Internal error -- this should never have happened since the while loop should have terminated first.") return self for (k, v,) in otherdict.iteritems(): assert self._assert_invariants() self[k] = v assert self._assert_invariants()
def _assert_invariants(self): _assert(len(self._lru) <= self._maxsize, "Size is required to be <= maxsize.") _assert(len(filter(lambda x: dict.has_key(self, x), self._lru)) == len(self._lru), "Each key in self._lru is required to be in dict.", filter(lambda x: not dict.has_key(self, x), self._lru), len(self._lru), self._lru, len(self), self) _assert(len(filter(lambda x: x in self._lru, self.keys())) == len(self), "Each key in dict is required to be in self._lru.", filter(lambda x: x not in self._lru, self.keys()), len(self._lru), self._lru, len(self), self) _assert(len(self._lru) == len(self), "internal consistency", filter(lambda x: x not in self.keys(), self._lru), len(self._lru), self._lru, len(self), self) _assert(len(self._lru) <= self._maxsize, "internal consistency", len(self._lru), self._lru, self._maxsize) return True
def bench_it(func, n, profile=False, profresults="pyutil-benchutil.prof"): if profile: st = time.realtime() cProfile.run('func(n)', profresults) sto = time.realtime() else: st = time.realtime() func(n) sto = time.realtime() timeelapsed = sto - st if timeelapsed <= 0: raise BadMeasure(timeelapsed) global worstemptymeasure emsta = time.realtime() do_nothing(2**32) emstop = time.realtime() empty = emstop - emsta if empty > worstemptymeasure: worstemptymeasure = empty _assert( timeelapsed * MARGINOFERROR > worstemptymeasure, "Invoking func %s(%s) took only %0.20f seconds, but we cannot accurately measure times much less than %0.20f seconds. Therefore we cannot produce good results here. Try benchmarking a more time-consuming variant." % ( func, n, timeelapsed, worstemptymeasure, )) return timeelapsed
def _assert_invariants(self): def lliterkeys(self): cur = self.first while cur != None: cur2 = cur.next yield cur.me[0] cur = cur2 def lllen(self): # Ugh. acc = 0 for x in lliterkeys(self): acc += 1 return acc def llhaskey(self, key): # Ugh. for x in lliterkeys(self): if x is key: return True return False for k in lliterkeys(self): _assert(self.d.has_key(k), "Each key in the linked list is required to be in the dict.", k) for k in self.d.iterkeys(): _assert(llhaskey(self, k), "Each key in the dict is required to be in the linked list.", k) _assert(lllen(self) == len(self.d), "internal consistency", self, self.d) _assert(len(self.d) <= self._maxsize, "Size is required to be <= maxsize.") return True
def bench_it(func, n, profile=False, profresults="pyutil-benchutil.prof"): if profile: st = time.realtime() cProfile.run('func(n)', profresults) sto = time.realtime() else: st = time.realtime() func(n) sto = time.realtime() timeelapsed = sto - st if timeelapsed <= 0: raise BadMeasure(timeelapsed) global worstemptymeasure emsta = time.realtime() do_nothing(2**32) emstop = time.realtime() empty = emstop - emsta if empty > worstemptymeasure: worstemptymeasure = empty _assert(timeelapsed*MARGINOFERROR > worstemptymeasure, "Invoking func %s(%s) took only %0.20f seconds, but we cannot accurately measure times much less than %0.20f seconds. Therefore we cannot produce good results here. Try benchmarking a more time-consuming variant." % (func, n, timeelapsed, worstemptymeasure,)) return timeelapsed
def _assert_invariants(self): _assert(len(self.d) <= self.m, "Size is required to be <= maxsize.", len(self.d), self.m) _assert((len(self.d) > 2) == (self.d[self.hs][2] is not self.ts) == (self.d[self.ts][1] is not self.hs), "Head and tail point to something other than each other if and only if there is at least one element in the dictionary.", self.hs, self.ts, len(self.d)) foundprevsentinel = 0 foundnextsentinel = 0 for (k, (v, p, n,)) in self.d.iteritems(): _assert(v not in (self.hs, self.ts,)) _assert(p is not self.ts, "A reference to the tail sentinel may not appear in prev.", k, v, p, n) _assert(n is not self.hs, "A reference to the head sentinel may not appear in next.", k, v, p, n) _assert(p in self.d, "Each prev is required to appear as a key in the dict.", k, v, p, n) _assert(n in self.d, "Each next is required to appear as a key in the dict.", k, v, p, n) if p is self.hs: foundprevsentinel += 1 _assert(foundprevsentinel <= 2, "No more than two references to the head sentinel may appear as a prev.", k, v, p, n) if n is self.ts: foundnextsentinel += 1 _assert(foundnextsentinel <= 2, "No more than one reference to the tail sentinel may appear as a next.", k, v, p, n) _assert(foundprevsentinel == 2, "A reference to the head sentinel is required appear as a prev (plus a self-referential reference).") _assert(foundnextsentinel == 2, "A reference to the tail sentinel is required appear as a next (plus a self-referential reference).") count = 0 for (k, v,) in self.iteritems(): _assert(k not in (self.hs, self.ts,)) count += 1 _assert(count == len(self.d)-2, count, len(self.d)) # -2 for the sentinels return True
else: tls.append(tl) if len(tls) == 0: raise Exception( "Couldn't get any measurements within time limits or number-of-attempts limits. Maybe something is wrong with your clock? %s" % (bmes, )) sumtls = reduce(operator.__add__, tls) mean = sumtls / len(tls) tls.sort() worst = tls[-1] best = tls[0] _assert( best > worstemptymeasure * MARGINOFERROR, "%s(n=%s) took %0.10f seconds, but we cannot measure times much less than about %0.10f seconds. Try a more time-consuming variant (such as higher n)." % ( func, n, best, worstemptymeasure * MARGINOFERROR, )) m = len(tls) / 4 if m > 0: mthbest = tls[m - 1] mthworst = tls[-m] else: mthbest = tls[0] mthworst = tls[-1] # The +/-0 index is the best/worst, the +/-1 index is the 2nd-best/worst, # etc, so we use mp1 to name it. mp1 = m + 1
if initfunc: initfunc(n) try: tl = bench_it(func, n, profile=profile, profresults=profresults) except BadMeasure, bme: bmes.append(bme) else: tls.append(tl) if len(tls) == 0: raise Exception("Couldn't get any measurements within time limits or number-of-attempts limits. Maybe something is wrong with your clock? %s" % (bmes,)) sumtls = reduce(operator.__add__, tls) mean = sumtls / len(tls) tls.sort() worst = tls[-1] best = tls[0] _assert(best > worstemptymeasure*MARGINOFERROR, "%s(n=%s) took %0.10f seconds, but we cannot measure times much less than about %0.10f seconds. Try a more time-consuming variant (such as higher n)." % (func, n, best, worstemptymeasure*MARGINOFERROR,)) m = len(tls)/4 if m > 0: mthbest = tls[m-1] mthworst = tls[-m] else: mthbest = tls[0] mthworst = tls[-1] # The +/-0 index is the best/worst, the +/-1 index is the 2nd-best/worst, # etc, so we use mp1 to name it. mp1 = m+1 res = { 'worst': mult(worst, UNITS_PER_SECOND)/n, 'best': mult(best, UNITS_PER_SECOND)/n, 'mp1': mp1,