-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change TimeStatistic to object instead of class
- Loading branch information
Showing
7 changed files
with
38 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,27 @@ | ||
import time | ||
from threading import current_thread, Lock | ||
|
||
from pandas import DataFrame | ||
|
||
from Common.Recoder import Recorder | ||
|
||
|
||
class TimeStatistic: | ||
_start_time = {} | ||
_count = {} | ||
_total_time = {} | ||
_lock = Lock() | ||
|
||
@classmethod | ||
def start(cls, name): | ||
with cls._lock: | ||
thread_id = current_thread().ident | ||
cls._start_time[(thread_id, name)] = time.time() | ||
|
||
@classmethod | ||
def end(cls, name, recorder: Recorder, output=True): | ||
with cls._lock: | ||
thread_id = current_thread().ident | ||
if (thread_id, name) in cls._start_time: | ||
inc_time = time.time() - cls._start_time[(thread_id, name)] | ||
cls._add_time(name, inc_time) | ||
del cls._start_time[(thread_id, name)] | ||
else: | ||
raise RuntimeError("end but no start") | ||
|
||
if output: | ||
recorder.info(f"{name} cost time: {inc_time}") | ||
|
||
@classmethod | ||
def _add_time(cls, name, inc_time): | ||
if name not in cls._total_time: | ||
cls._total_time[name] = 0 | ||
cls._count[name] = 0 | ||
cls._total_time[name] += inc_time | ||
cls._count[name] += 1 | ||
|
||
@classmethod | ||
def add_time(cls, name, inc_time): | ||
with cls._lock: | ||
cls._add_time(name, inc_time) | ||
|
||
@classmethod | ||
def clear(cls): | ||
cls._start_time = {} | ||
cls._count = {} | ||
cls._total_time = {} | ||
cls._lock = Lock() | ||
def __init__(self): | ||
self._start_time = {} | ||
self._count = {} | ||
self._total_time = {} | ||
|
||
def start(self, name): | ||
self._start_time[name] = time.time() | ||
|
||
def end(self, name): | ||
if name in self._start_time: | ||
inc_time = time.time() - self._start_time[name] | ||
self._add_time(name, inc_time) | ||
del self._start_time[name] | ||
else: | ||
raise RuntimeError(f"TimeStatistic: {name} not started") | ||
return str(inc_time) | ||
|
||
def _add_time(self, name, inc_time): | ||
if name not in self._total_time: | ||
self._total_time[name] = 0 | ||
self._count[name] = 0 | ||
self._total_time[name] += inc_time | ||
self._count[name] += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters