本文整理汇总了Python中timeit.Timer.timer方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.timer方法的具体用法?Python Timer.timer怎么用?Python Timer.timer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类timeit.Timer
的用法示例。
在下文中一共展示了Timer.timer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sample
# 需要导入模块: from timeit import Timer [as 别名]
# 或者: from timeit.Timer import timer [as 别名]
def sample(self, nsamples, acc_ratio, queue_size=10000):
queue = multiprocessing.Queue(queue_size)
t_high = multiprocessing.Value(c_double, sys.float_info.max)
ndiscards = [multiprocessing.Value(c_int, 0)\
for ii in range(self.nworkers)]
workers = [Sampler(queue, ndiscards[ii], t_high,
self.f_prior,
self.f_statistics,
self.f_distance)
for ii in range(self.nworkers)]
for w in workers:
w.start()
debug("Starting calibration of the queue...\n")
ntest = max(1000, int(2.0/acc_ratio))
samples = [queue.get()[1] for ii in range(ntest)]
debug("Calibration done\n")
t_high.value = sorted(samples)[int(2*ntest*acc_ratio)]
samples = []
debug("Starting sampling\n")
nvalids = 0
clock = Timer()
log_time = clock.timer()
while True:
now = clock.timer()
if now - log_time > .1 :
log_time = now
nd = sum([ndiscards[ii].value for ii in range(self.nworkers)])
ndraws = nd + nvalids
debug("Sampling " +
str(int(ndraws / (nsamples/100))) +
"% complete\r")
if ndraws > nsamples:
break
params, dist = queue.get()
nvalids += 1
samples.append((params, dist))
for w in workers:
w.terminate()
debug("Sampling completed \n")
debug("Queue size: " + str(nvalids) + '\n')
#TODO
# Make sure the queue is fully consumed, otherwise
# sample statistics are invalid
if len(samples) < nsamples*acc_ratio:
raise BufferError("Incomplete sample, only %i available" \
% len(samples))
results = [x[0] for x in sorted(samples,\
key=lambda x: x[1])\
[0:int(nsamples*acc_ratio)]]
return results