-
Notifications
You must be signed in to change notification settings - Fork 2
/
profiler.py
73 lines (60 loc) · 2.23 KB
/
profiler.py
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
############################################################################
#
# profiler.py - Rev 1.0
# Copyright (C) 2021 by Joseph B. Attili, aa2il AT arrl DOT net
#
# Some functions to assist in identifying computational bottlenecks.
#
############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
############################################################################
import cProfile
import time
############################################################################
class Profiler:
def __init__(self,P):
self.pr = cProfile.Profile()
self.enabled = False
self.P = P
self.dT = P.OUT_CHUNK_SIZE/float(P.FS_OUT)
self.ps = self.pr.print_stats
def profile(self,txt=''):
if self.enabled:
self.pr.disable()
self.enabled = False
print('\n',txt,' - Profile for chunk',self.P.nchunks,':')
print('\tNominal frame time =',self.dT,' secs.\n')
self.ps(sort='time')
elif (self.P.nchunks % 100) == 0:
self.pr.enable()
self.enabled = True
class Profiler2:
def __init__(self):
self.pr = cProfile.Profile()
self.enabled = False
self.triggered = False
self.ps = self.pr.print_stats
def start(self,txt=''):
self.pr.enable()
self.enabled = True
self.triggered = True
self.t1 = time.time()
def stop(self,txt=''):
if self.enabled:
t2 = time.time()
dT = t2-self.t1
self.pr.disable()
self.enabled = False
print('\n',txt,' - Profile for chunk:')# ,self.P.nchunks,':'
print('\tNominal elapsed time =',dT,'secs. =',1000.*dT,'ms\n')
self.ps(sort='time')