Returns the performance statistics (as a string) of the given Python function.
(function, *args, **kwargs)
| 71 | return time() - t |
| 72 | |
| 73 | def profile(function, *args, **kwargs): |
| 74 | """ Returns the performance statistics (as a string) of the given Python function. |
| 75 | """ |
| 76 | def run(): |
| 77 | function(*args, **kwargs) |
| 78 | try: |
| 79 | import cProfile as profile |
| 80 | except: |
| 81 | import profile |
| 82 | import pstats |
| 83 | import os |
| 84 | import sys; sys.modules["__main__"].__profile_run__ = run |
| 85 | id = function.__name__ + "()" |
| 86 | profile.run("__profile_run__()", id) |
| 87 | p = pstats.Stats(id) |
| 88 | p.stream = open(id, "w") |
| 89 | p.sort_stats("time").print_stats(20) |
| 90 | p.stream.close() |
| 91 | s = open(id).read() |
| 92 | os.remove(id) |
| 93 | return s |
| 94 | |
| 95 | #### PRECISION & RECALL ############################################################################ |
| 96 |