Matt Chaput avatar Matt Chaput committed b17e714

Added Timing manager.

Comments (0)

Files changed (1)

src/whoosh/support/testing.py

         return fstore.create_index(self.schema, indexname=self.basename)
 
 
+class Timing(object):
+    def __init__(self, name=None, number=1, compare=None):
+        self.name = name
+        self.number = number
+        self.compare = compare
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tv):
+        if exc_type is None:
+            import inspect, timeit
+
+            frame = inspect.currentframe(1)
+            timed_fn = frame.f_locals["_"]
+            self.runtime = timeit.timeit(timed_fn, number=self.number)
+            sys.stdout.write("%s: %0.08f\n" % (self.name, self.runtime))
+
+            if self.compare:
+                ctime = self.compare.runtime
+                sys.stdout.write(" " * len(self.name) + ": ")
+                sys.stdout.write("%0.04f %0.04f\n" % (self.runtime / ctime,
+                                                      ctime / self.runtime))
+
+
 def skip_if(cond):
     """A Nose test decorator that skips the decorated test if the given
     function returns True at runtime.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.