-
Notifications
You must be signed in to change notification settings - Fork 1
/
stop_watch.py.364464503.filtered.1514276135.formatted
executable file
·107 lines (91 loc) · 3.34 KB
/
stop_watch.py.364464503.filtered.1514276135.formatted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Python motu client v.1.0.3
#
# Motu, a high efficient, robust and Standard compliant Web Server for Geographic
# Data Dissemination.
#
# http://cls-motu.sourceforge.net/
#
# (C) Copyright 2009-2010, by CLS (Collecte Localisation Satellites) -
# http://www.cls.fr - and Contributors
#
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
from threading import Thread, local
import time
import threading
# global stats
tsl = local()
class StopWatch(object):
TIME = "time"
GLOBAL = "global"
def __init__(self):
# contains the computed times
self.times = {}
# contains the current timers
self.timers = {}
def clear(self):
self.timers = {}
self.times = {}
def start(self,label = GLOBAL):
"""Starts a new counter
Returns the time the counter has been recorded.
"""
self.timers[label] = self.__time()
return self.timers[label]
def stop(self,label=GLOBAL):
"""Stops the clock for the given counter.
Returns the time at which the instance was stopped.
"""
self.times[label] = self.elapsed(label)
del self.timers[label]
return self.times[label]
def isRunning(self, label=GLOBAL):
return label in self.timers
def elapsed(self,label=GLOBAL):
"""The number of seconds since the current time that the StopWatch
object was created. If stop() was called, it is the number
of seconds from the instance creation until stop() was called.
"""
t0 = self.times[label] if label in self.times else 0.
t1 = self.timers[label] if label in self.timers else 0.
t2 = self.__time() if label in self.timers else 0.
return t0 + (t2 - t1)
def getTimes(self):
return self.times
def __time(self):
"""Wrapper for time.time() to allow unit testing.
"""
return time.time()
def __str__(self):
"""Nicely format the elapsed time
"""
keys = self.times.keys() + filter( lambda x:x not in self.times.keys(), self.timers.keys() )
txt = ""
for key in keys:
txt = txt + key + " : " + str(self.elapsed(key)) + " s " + ("(running)" if self.isRunning(key) else "(stopped)")+"\n"
return txt
def localThreadStopWatch():
if not hasattr(tsl,'timer'):
lock = threading.Lock()
lock.acquire()
try:
if not hasattr(tsl,'timer'):
tsl.timer = StopWatch()
finally:
lock.release()
return tsl.timer