def _get_time_times(timer=os.times):
t = timer()
return t[0] + t[1]
# Using getrusage(3) is better than clock(3) if available:
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
# Furthermore, on a POSIX system, returns microseconds, which
# wrap around after 36min.
python类times()的实例源码
def run_during(duration, func):
_t = time.time
n = 0
start = os.times()
start_timestamp = _t()
real_start = start[4] or start_timestamp
while True:
func()
n += 1
if _t() - start_timestamp > duration:
break
end = os.times()
real = (end[4] if start[4] else time.time()) - real_start
return n, real, sum(end[0:2]) - sum(start[0:2])
def TSTOP(*label):
global t0, t1
u, s, cu, cs = os.times()
t1 = u+cu, s+cs, time.time()
tt = []
for i in range(3):
tt.append(t1[i] - t0[i])
[u, s, r] = tt
msg = ''
for x in label: msg = msg + (x + ' ')
msg = msg + '%r user, %r sys, %r real\n' % (u, s, r)
sys.stderr.write(msg)
def _get_time_times(timer=os.times):
t = timer()
return t[0] + t[1]
# Using getrusage(3) is better than clock(3) if available:
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
# Furthermore, on a POSIX system, returns microseconds, which
# wrap around after 36min.
def run_during(duration, func):
_t = time.time
n = 0
start = os.times()
start_timestamp = _t()
real_start = start[4] or start_timestamp
while True:
func()
n += 1
if _t() - start_timestamp > duration:
break
end = os.times()
real = (end[4] if start[4] else time.time()) - real_start
return n, real, sum(end[0:2]) - sum(start[0:2])
def _get_time_times(timer=os.times):
t = timer()
return t[0] + t[1]
# Using getrusage(3) is better than clock(3) if available:
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
# Furthermore, on a POSIX system, returns microseconds, which
# wrap around after 36min.
def initStats(self):
sNode.n = 0
StateSpace.n = 1 #initial state already generated on call so search
self.cycle_check_pruned = 0
self.cost_bound_pruned = 0
self.total_search_time = 0
self.total_search_time = os.times()[0]
def main():
print(os.nice(0)) # get relative process priority
print(os.nice(1)) # change relative priority
print(os.times()) # process times: system, user etc...
print(os.isatty(0)) # is the file descriptor arg a tty?(0 = stdin)
print(os.isatty(4)) # 4 is just an arbitrary test value
print(os.getloadavg()) # UNIX only - number of processes in queue
print(os.cpu_count()) # New in Python 3.4
input_ff_rec_robot_nengo_directu_ocl.py 文件源码
项目:FOLLOW
作者: adityagilra
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def sim_run_flush(tFlush,nFlush):
'''
Run simulation for nFlush*tFlush seconds,
Flush probes every tFlush of simulation time,
(only flush those that don't have 'weights' in their label names)
'''
weighttimeidxold = 0
#doubledLearningRate = False
for duration in [tFlush]*nFlush:
_,_,_,_,realtime = os.times()
print("Finished till",sim.time,'s, in',realtime-realtimeold,'s')
sys.stdout.flush()
# save weights if weightdt or more has passed since last save
weighttimeidx = int(sim.time/weightdt)
if weighttimeidx > weighttimeidxold:
weighttimeidxold = weighttimeidx
save_current_weights(False,sim.time)
# flush probes
for probe in sim.model.probes:
# except weight probes (flushed in save_current_weights)
# except error probe which is saved fully in ..._end.shelve
if probe.label is not None:
if 'weights' in probe.label or 'error' in probe.label:
break
del sim._probe_outputs[probe][:]
## if time > 1000s, double learning rate
#if sim.time>1000. and not doubledLearningRate:
# changeLearningRate(4.) # works only if excPESDecayRate = None
# doubledLearningRate = True
# run simulation for tFlush duration
sim.run(duration,progress_bar=False)
###
### run the simulation, with flushing for learning simulations ###
###
def sim_run_flush(tFlush,nFlush):
'''
Run simulation for nFlush*tFlush seconds,
Flush probes every tFlush of simulation time,
(only flush those that don't have 'weights' in their label names)
'''
weighttimeidxold = 0
#doubledLearningRate = False
for duration in [tFlush]*nFlush:
_,_,_,_,realtime = os.times()
print("Finished till",sim.time,'s, in',realtime-realtimeold,'s')
sys.stdout.flush()
# save weights if weightdt or more has passed since last save
weighttimeidx = int(sim.time/weightdt)
if weighttimeidx > weighttimeidxold:
weighttimeidxold = weighttimeidx
save_current_weights(False,sim.time)
# flush probes
for probe in sim.model.probes:
# except weight probes (flushed in save_current_weights)
# except error probe which is saved fully in ..._end.shelve
if probe.label is not None:
if 'weights' in probe.label or 'error' in probe.label:
break
del sim._probe_outputs[probe][:]
## if time > 1000s, double learning rate
#if sim.time>1000. and not doubledLearningRate:
# changeLearningRate(4.) # works only if excPESDecayRate = None
# doubledLearningRate = True
# run simulation for tFlush duration
sim.run(duration,progress_bar=False)
###
### run the simulation, with flushing for learning simulations ###
###
def sim_run_flush(tFlush,nFlush):
'''
Run simulation for nFlush*tFlush seconds,
Flush probes every tFlush of simulation time,
(only flush those that don't have 'weights' in their label names)
'''
weighttimeidxold = 0
#doubledLearningRate = False
for duration in [tFlush]*nFlush:
_,_,_,_,realtime = os.times()
print("Finished till",sim.time,'s, in',realtime-realtimeold,'s')
sys.stdout.flush()
# save weights if weightdt or more has passed since last save
weighttimeidx = int(sim.time/weightdt)
if weighttimeidx > weighttimeidxold:
weighttimeidxold = weighttimeidx
save_current_weights(False,sim.time)
# flush probes
for probe in sim.model.probes:
# except weight probes (flushed in save_current_weights)
# except error probe which is saved fully in ..._end.shelve
if probe.label is not None:
if 'weights' in probe.label or 'error' in probe.label:
break
del sim._probe_outputs[probe][:]
## if time > 1000s, double learning rate
#if sim.time>1000. and not doubledLearningRate:
# changeLearningRate(4.) # works only if excPESDecayRate = None
# doubledLearningRate = True
# run simulation for tFlush duration
sim.run(duration,progress_bar=False)
###
### run the simulation, with flushing for learning simulations ###
###
def _get_time_times(timer=os.times):
t = timer()
return t[0] + t[1]
# Using getrusage(3) is better than clock(3) if available:
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
# Furthermore, on a POSIX system, returns microseconds, which
# wrap around after 36min.
def threads(self):
"""Return threads opened by process as a list of
(id, user_time, system_time) namedtuples representing
thread id and thread CPU times (user/system).
On OpenBSD this method requires root access.
"""
return self._proc.threads()
def cpu_times(self):
"""Return a (user, system, children_user, children_system)
namedtuple representing the accumulated process time, in
seconds.
This is similar to os.times() but per-process.
On OSX and Windows children_user and children_system are
always set to 0.
"""
return self._proc.cpu_times()
def _cpu_busy_time(times):
"""Given a cpu_time() ntuple calculates the busy CPU time.
We do so by subtracting all idle CPU times.
"""
busy = _cpu_tot_time(times)
busy -= times.idle
# Linux: "iowait" is time during which the CPU does not do anything
# (waits for IO to complete). On Linux IO wait is *not* accounted
# in "idle" time so we subtract it. Htop does the same.
# References:
# https://github.com/torvalds/linux/blob/
# 447976ef4fd09b1be88b316d1a81553f1aa7cd07/kernel/sched/cputime.c#L244
busy -= getattr(times, "iowait", 0)
return busy
def get_user_cputime(self):
return os.times()[0]
def burn_cpu(self):
"""Consume REQUEST_CPUTIME_SEC core seconds.
This method consumes REQUEST_CPUTIME_SEC core seconds. If unable to
complete within REQUEST_TIMEOUT_SEC walltime seconds, it times out and
terminates the process.
"""
start_walltime_sec = self.get_walltime()
start_cputime_sec = self.get_user_cputime()
while (self.get_user_cputime() <
start_cputime_sec + REQUEST_CPUTIME_SEC):
self.busy_wait()
if (self.get_walltime() >
start_walltime_sec + REQUEST_TIMEOUT_SEC):
sys.exit(1)
def __init__(self, timer=None, bias=None):
self.timings = {}
self.cur = None
self.cmd = ""
self.c_func_name = ""
if bias is None:
bias = self.bias
self.bias = bias # Materialize in local dict for lookup speed.
if not timer:
self.timer = self.get_time = time.process_time
self.dispatcher = self.trace_dispatch_i
else:
self.timer = timer
t = self.timer() # test out timer function
try:
length = len(t)
except TypeError:
self.get_time = timer
self.dispatcher = self.trace_dispatch_i
else:
if length == 2:
self.dispatcher = self.trace_dispatch
else:
self.dispatcher = self.trace_dispatch_l
# This get_time() implementation needs to be defined
# here to capture the passed-in timer in the parameter
# list (for performance). Note that we can't assume
# the timer() result contains two values in all
# cases.
def get_time_timer(timer=timer, sum=sum):
return sum(timer())
self.get_time = get_time_timer
self.t = self.get_time()
self.simulate_call('profiler')
# Heavily optimized dispatch routine for os.times() timer
def _get_time_times(timer=os.times):
t = timer()
return t[0] + t[1]
# Using getrusage(3) is better than clock(3) if available:
# on some systems (e.g. FreeBSD), getrusage has a higher resolution
# Furthermore, on a POSIX system, returns microseconds, which
# wrap around after 36min.