def rep_bench(func, n, initfunc=None, MAXREPS=10, MAXTIME=60.0, profile=False, profresults="pyutil-benchutil.prof", UNITS_PER_SECOND=1): """ Will run the func up to MAXREPS times, but won't start a new run if MAXTIME (wall-clock time) has already elapsed (unless MAXTIME is None). """ assert isinstance(n, int), (n, type(n)) starttime = time.realtime() tls = [] # elapsed time in nanoseconds while ((len(tls) < MAXREPS) or (MAXREPS is None)) and ((MAXTIME is None) or ((time.realtime() - starttime) < MAXTIME)): if initfunc: initfunc(n) try: tl = bench_it(func, n, profile=profile, profresults=profresults) except BadMeasure: pass else: tls.append(tl * UNITS_PER_SECOND) assert tls sumtls = reduce(operator.__add__, tls) mean = sumtls / len(tls) tls.sort() worst = tls[-1] best = tls[0] m = min(int(math.log(len(tls))) + 1, len(tls)) mthworst = tls[-m] mthbest = tls[m - 1] res = { 'worst': worst / n, 'best': best / n, 'm': m, 'mth-best': mthbest / n, 'mth-worst': mthworst / n, 'mean': mean / n, 'num': len(tls), } print "best: %(best)#8.03e, %(m)3dth-best: %(mth-best)#8.03e, mean: %(mean)#8.03e, %(m)3dth-worst: %(mth-worst)#8.03e, worst: %(worst)#8.03e (of %(num)6d)" % res return res
def force_repeatability(): now = 1043659734.0 def faketime(): global tdelta tdelta += 1 return now + tdelta time.faketime = faketime time.time = faketime from idlib import i2b def fakeurandom(n): if n > 64: raise ( "Can't produce more than 64 bytes of pseudorandomness efficiently." ) elif n == 0: return '' else: z = i2b(random.getrandbits(n * 8)) x = z + "0" * (n - len(z)) assert len(x) == n return x os.fakeurandom = fakeurandom os.urandom = fakeurandom global seeded if not seeded: SEED = os.environ.get('REPEATABLE_RANDOMNESS_SEED', None) if SEED is None: # Generate a seed which is integral and fairly short (to ease cut-and-paste, writing it down, etc.). t = time.realtime() subsec = t % 1 t += (subsec * 1000000) t %= 1000000 SEED = long(t) import sys sys.stdout.write("REPEATABLE_RANDOMNESS_SEED: %s\n" % SEED) sys.stdout.flush() sys.stdout.write( "In order to reproduce this run of the code, set the environment variable \"REPEATABLE_RANDOMNESS_SEED\" to %s before executing.\n" % SEED) sys.stdout.flush() random.seed(SEED) def seed_which_refuses(a): sys.stdout.write("I refuse to reseed to %s. Go away!\n" % (a, )) sys.stdout.flush() return random.realseed = random.seed random.seed = seed_which_refuses seeded = True import setutil setutil.RandomSet.DETERMINISTIC = True
def rep_bench(func, n, initfunc=None, MAXREPS=10, MAXTIME=60.0, profile=False, profresults="pyutil-benchutil.prof", UNITS_PER_SECOND=1): """ Will run the func up to MAXREPS times, but won't start a new run if MAXTIME (wall-clock time) has already elapsed (unless MAXTIME is None). """ assert isinstance(n, int), (n, type(n)) starttime = time.realtime() tls = [] # elapsed time in nanoseconds while ((len(tls) < MAXREPS) or (MAXREPS is None)) and ((MAXTIME is None) or ((time.realtime() - starttime) < MAXTIME)): if initfunc: initfunc(n) try: tl = bench_it(func, n, profile=profile, profresults=profresults) except BadMeasure: pass else: tls.append(tl * UNITS_PER_SECOND) assert tls sumtls = reduce(operator.__add__, tls) mean = sumtls / len(tls) tls.sort() worst = tls[-1] best = tls[0] m = min(int(math.log(len(tls)))+1, len(tls)) mthworst = tls[-m] mthbest = tls[m-1] res = { 'worst': worst/n, 'best': best/n, 'm': m, 'mth-best': mthbest/n, 'mth-worst': mthworst/n, 'mean': mean/n, 'num': len(tls), } print "best: %(best)#8.03e, %(m)3dth-best: %(mth-best)#8.03e, mean: %(mean)#8.03e, %(m)3dth-worst: %(mth-worst)#8.03e, worst: %(worst)#8.03e (of %(num)6d)" % res return res
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 setUp(self, repeatable=False): """ @param repeatable: install the repeatable_randomness hacks to attempt to without access to real randomness and real time.time from the code under test """ self.repeatable = repeatable if self.repeatable: import repeatable_random repeatable_random.force_repeatability() if hasattr(time, 'realtime'): self.teststarttime = time.realtime() else: self.teststarttime = time.time()
def force_repeatability(): now = 1043659734.0 def faketime(): global tdelta tdelta += 1 return now + tdelta time.faketime = faketime time.time = faketime from idlib import i2b def fakeurandom(n): if n > 64: raise ("Can't produce more than 64 bytes of pseudorandomness efficiently.") elif n == 0: return '' else: z = i2b(random.getrandbits(n*8)) x = z + "0" * (n-len(z)) assert len(x) == n return x os.fakeurandom = fakeurandom os.urandom = fakeurandom global seeded if not seeded: SEED = os.environ.get('REPEATABLE_RANDOMNESS_SEED', None) if SEED is None: # Generate a seed which is integral and fairly short (to ease cut-and-paste, writing it down, etc.). t = time.realtime() subsec = t % 1 t += (subsec * 1000000) t %= 1000000 SEED = long(t) import sys sys.stdout.write("REPEATABLE_RANDOMNESS_SEED: %s\n" % SEED) ; sys.stdout.flush() sys.stdout.write("In order to reproduce this run of the code, set the environment variable \"REPEATABLE_RANDOMNESS_SEED\" to %s before executing.\n" % SEED) ; sys.stdout.flush() random.seed(SEED) def seed_which_refuses(a): sys.stdout.write("I refuse to reseed to %s. Go away!\n" % (a,)) ; sys.stdout.flush() return random.realseed = random.seed random.seed = seed_which_refuses seeded = True import setutil setutil.RandomSet.DETERMINISTIC = 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
import sys from time import time as realtime from pytools_uibcdf.Time import formatted_elapsed_time, formatted_local_time import molmodmt as m3t import simtk.openmm as mm import simtk.openmm.app as app import simtk.unit as unit from mdtraj.reporters import HDF5Reporter from openmmtools.integrators import LangevinIntegrator #### Log file logfile = open('logfile.txt', 'w') #logfile = sys.stdout start_realtime = realtime() logfile.write("\n") logfile.write("Start: " + formatted_local_time() + "\n") logfile.write("\n") #### Loading PDB pdb = m3t.convert('system_equilibrated_NVT.pdb', 'openmm.PDBFile') #### System topology = m3t.convert(pdb, 'openmm.Topology') forcefield = app.ForceField('amber99sbildn.xml', 'tip3p.xml') system = forcefield.createSystem(topology, nonbondedMethod=app.PME, nonbondedCutoff=1.2 * unit.nanometers,
def time(): global _time, _lastTime if (_time == None): _time = realtime() _lastTime = realtime() return _time + (_lastTime - _time) * _dialation