Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Util/Profiler.py
diff options
context:
space:
mode:
authorjaberg <james@localhost.localdomain>2007-01-07 21:24:04 (GMT)
committer jaberg <james@localhost.localdomain>2007-01-07 21:24:04 (GMT)
commit1becac24dfa6bd0cee4ae46f4c0b8630bfbb6269 (patch)
tree70d602342d546fd8c268143b54d46b8ca580a7ed /Util/Profiler.py
parent2a940a71469b2b5964e8deab3ed6db0254424671 (diff)
unify_edit init
Diffstat (limited to 'Util/Profiler.py')
-rw-r--r--Util/Profiler.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/Util/Profiler.py b/Util/Profiler.py
new file mode 100644
index 0000000..338589e
--- /dev/null
+++ b/Util/Profiler.py
@@ -0,0 +1,76 @@
+
+import time
+
+class TaskProfiler( object ):
+ def __init__( self ):
+ self.profiles = {}
+
+ def ProfileBegin( self, profile ):
+ if self.profiles.has_key(profile) == False: self.profiles[profile] = TaskProfile(profile)
+ self.profiles[profile].begin()
+
+ def ProfileEnd( self, profile ):
+ if self.profiles.has_key(profile) == False: return False
+ self.profiles[profile].end()
+
+ def Profile(self, profile):
+ if profile in self.profiles:
+ if self.profiles[profile].inProgress:
+ self.profiles[profile].end()
+ else:
+ self.profiles[profile].begin()
+ else:
+ self.profiles[profile] = TaskProfile(profile)
+ self.profiles[profile].begin()
+
+ def ProfilePrint( self, profile ):
+ if self.profiles.has_key(profile) == False: return "Couldn't find profile: " + profile
+ return self.profiles[profile].printoverall()
+
+ def ProfileEndAndPrint( self, profile ):
+ if self.profiles.has_key(profile) == False: return "Couldn't find profile: " + profile
+ self.profiles[profile].end()
+ return self.profiles[profile].printlast()
+
+
+ def PrintAll( self ):
+ str = ""
+ keys = self.profiles.keys()
+ keys.sort()
+ return "\n".join( [self.profiles[k].printoverall() for k in keys] )
+ #for p in self.profiles:
+ #str += "\n" + self.profiles[p].printoverall()
+ #return str
+
+
+class TaskProfile( object ):
+ def __init__( self, name ):
+ self.name = name
+ self.count = 0
+ self.min = 666666666.0 # really high
+ self.max = -1.0 # pretty low
+ self.avg = 0.0
+ self.dt = 0
+ self.inProgress = False;
+
+ def begin( self ):
+ if self.inProgress: return False # you fucked up your ProfileBegin and ProfileEnd pairs
+ self.inProgress = True
+ self.startTime = time.time()
+
+ def end( self ):
+ self.dt = time.time() - self.startTime
+ if self.dt > self.max: self.max = self.dt
+ if self.dt < self.min: self.min = self.dt
+ self.avg = (self.dt + self.avg*self.count)/(self.count+1)
+ self.count += 1
+ self.inProgress = False
+
+ def printlast( self ):
+ return "Profile: " + self.name + " last dt: %f" % (self.dt)
+
+ def printoverall( self ):
+ if self.count == 0: return "Profile: " + self.name + ", no data!"
+ return "Profile: " + self.name + " avg: %f max: %f min: %f count: %d" % (self.avg, self.max, self.min, self.count)
+
+TP = TaskProfiler()