[pitivi] utils: New profiling decorator



commit 36455632c9a918280fef1d890aca1cbc92236d50
Author: Edward Hervey <bilboed bilboed com>
Date:   Sun Sep 6 14:57:47 2009 +0200

    utils: New profiling decorator
    
    Use as such:
    @profile
    def my_function_or_method(blah):
     ....

 pitivi/utils.py |   17 +++++++++++++++++
 1 files changed, 17 insertions(+), 0 deletions(-)
---
diff --git a/pitivi/utils.py b/pitivi/utils.py
index 7bd8521..26e8141 100644
--- a/pitivi/utils.py
+++ b/pitivi/utils.py
@@ -30,6 +30,7 @@ import os
 from pitivi.signalinterface import Signallable
 import pitivi.log.log as log
 from gettext import ngettext
+import cProfile
 
 UNKNOWN_DURATION = 2 ** 63 - 1
 
@@ -462,3 +463,19 @@ class CachedFactoryList(object):
         # invalidate the cache
         log.warning("utils", "New feature added, invalidating cached factories")
         self._factories = None
+
+def profile(func, profiler_filename="result.prof"):
+    import os.path
+    counter = 1
+    output_filename = profiler_filename
+    while os.path.exists(output_filename):
+        output_filename = profiler_filename + str(counter)
+        counter += 1
+
+    def _wrapper(*args,**kwargs):
+        local_func = func
+        cProfile.runctx("result = local_func(*args, **kwargs)", globals(), locals(),
+                        filename=output_filename)
+        return locals()["result"]
+
+    return _wrapper



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]