当前位置: 首页>>代码示例>>Python>>正文


Python Timer.timer方法代码示例

本文整理汇总了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
开发者ID:pbourguignon,项目名称:python-abc,代码行数:64,代码来源:ABCmp.py


注:本文中的timeit.Timer.timer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。