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


Python Stopwatch.duration方法代码示例

本文整理汇总了Python中stopwatch.Stopwatch.duration方法的典型用法代码示例。如果您正苦于以下问题:Python Stopwatch.duration方法的具体用法?Python Stopwatch.duration怎么用?Python Stopwatch.duration使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stopwatch.Stopwatch的用法示例。


在下文中一共展示了Stopwatch.duration方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_sanity

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
    def test_sanity(self):
        lock1 = threading.RLock()
        lock2 = threading.RLock()
        
        def sleep_and_inc(lock,sleep_time): 
            with synchronize_using(lock):
                time.sleep(sleep_time)
                lock.sync_test_counter = getattr(lock,'sync_test_counter',0) + 1
                
        sleep_time = 0.5
        n = 4
        
        s = Stopwatch()
        lst_threads = []
        for lock in [lock1,lock2]:
            for _ in xrange(n):
                t = threading.Thread(target=sleep_and_inc,args=[lock,sleep_time])
                lst_threads.append(t)
                t.start()

        # wait for all threads, then check results
        for t in lst_threads:
            t.join()
            
        duration = s.duration()
        self.assertEqual(lock1.sync_test_counter,n)
        self.assertEqual(lock2.sync_test_counter,n)
        ideal_time = n*sleep_time
        self.assert_(ideal_time*0.9 < duration < ideal_time*1.1, "duration=%s, ideal=%s" % (duration,ideal_time))
开发者ID:idobarkan,项目名称:my-code,代码行数:31,代码来源:test_synchronization.py

示例2: test_n_threads

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
    def test_n_threads(self):
        sleep_time = 0.1

        class Sleeper(object):
            def __init__(self):
                self._lock = threading.RLock()  # used by @synchronized decorator
                self.i = 0
                self.thread_ids = set()

            @synchronized
            def _critical_section(self):
                self.thread_ids.add(id(threading.currentThread()))
                self.i += 1

            def sleep(self):
                time.sleep(sleep_time)
                self._critical_section()

        n_threads = 5
        n_calls = 20
        ao = AO.AO(Sleeper(), n_threads)
        ao.start()
        self.aos_to_stop.append(ao)

        s = Stopwatch()
        futures = [ao.sleep() for i in xrange(n_calls)]
        for f in futures:
            f.get()
        duration = s.duration()
        expected = sleep_time * n_calls / float(n_threads)
        self.assert_(0.9 * expected < duration < 1.2 * expected, "duration=%s, expected=%s" % (duration, expected))
        self.assertEqual(ao.obj.i, n_calls)
        self.assertEqual(len(ao.obj.thread_ids), n_threads)
        self.failIf(id(threading.currentThread()) in ao.obj.thread_ids)
开发者ID:giltayar,项目名称:Python-Exercises,代码行数:36,代码来源:test_AO.py

示例3: test_no_sync

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
 def test_no_sync(self):
     stopwatch = Stopwatch()
     threading.Thread(target=self.s.add,args=[10]).start()
     time.sleep(0.05) # make sure other thread gets head start
     val = self.s.add(5)
     self.assertEqual(val,0)
     
     duration = stopwatch.duration()
     self.assert_(duration < 1.2*self.s.sleep_time, 'calls took too long. duration=%s' % duration)
开发者ID:idobarkan,项目名称:my-code,代码行数:11,代码来源:test_synchronization.py

示例4: test_normal

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
 def test_normal(self):
     stopwatch = Stopwatch()
     threading.Thread(target=self.s.sync_add,args=[10]).start()
     time.sleep(0.05) # make sure other thread gets head start (and the mutex)
     val = self.s.sync_add(5)
     self.assertEqual(val,10)
     
     duration = stopwatch.duration()
     self.assert_(duration > 1.9*self.s.sleep_time, 'calls completed too quickly. duration=%s' % duration)
开发者ID:idobarkan,项目名称:my-code,代码行数:11,代码来源:test_synchronization.py

示例5: test_timeout

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
    def test_timeout(self):
        # preset future - should return within timeout
        f = future.Future.preset(3)
        self.assertEqual(f.get(100),3)

        # unset future - should raise exception
        timeout = 1 # timeout in seconds
        f = future.Future()
        stopwatch = Stopwatch()
        stopwatch.start()
        self.assertRaises(future.FutureTimeoutException,f.get,timeout*1000)
        duration = stopwatch.duration()
        self.assert_(0.9*timeout < duration < 1.3*timeout, 'duration=%s' % duration)  
开发者ID:idobarkan,项目名称:my-code,代码行数:15,代码来源:test_future.py

示例6: test_sanity

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
 def test_sanity(self):
     s = Stopwatch()
     time.sleep(0.5)
     duration = s.duration()
     self.assert_(0.45 < duration < 0.55, 'duration=%s' % duration)
开发者ID:giltayar,项目名称:Python-Exercises,代码行数:7,代码来源:test_stopwatch.py

示例7: firstn

# 需要导入模块: from stopwatch import Stopwatch [as 别名]
# 或者: from stopwatch.Stopwatch import duration [as 别名]
from stopwatch import Stopwatch


class firstn(object):
  def __init__(self, n):
    self.n = n
    self.num = 0 #    self.num, self.nums = 0, []

  def __iter__(self):
    return self

  def next(self):
    if self.num < self.n:
      cur, self.num = self.num, self.num+1
      return cur
    else:
      raise StopIteration()


      
s = Stopwatch()
sum_of_first_n = sum(firstn(10000000))
duration = s.duration()
print "sum:",sum_of_first_n
print "duration:",duration
开发者ID:AssafR,项目名称:sandbox,代码行数:27,代码来源:sum_of_n_iterator.py


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