Created
October 17, 2019 04:38
-
-
Save usuyama/e56ccc347b4654512d1bec91006c0c02 to your computer and use it in GitHub Desktop.
databricks line_profiler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dbutils.library.installPyPI("line_profiler") # this fails with Python 3.7 | |
import inspect | |
from io import StringIO | |
from line_profiler import LineProfiler | |
def profile_function(my_func, args, *kwargs): | |
lp = LineProfiler() | |
output_val = lp(my_func)(args, *kwargs) | |
mystdout = StringIO() | |
lp.print_stats(stream=mystdout) | |
#Redirect stdout so we can grab profile output | |
lprof_lines = mystdout.getvalue().split('\n') | |
profile_start = 1 + next(idx for idx, line in enumerate(lprof_lines) if '=====' in line) | |
lprof_code_lines = lprof_lines[profile_start:-1] | |
source_lines = inspect.getsource(my_func).split('\n') | |
if len(source_lines) != len(lprof_code_lines): | |
print("WARNING! Mismatch in source length and returned line profiler estimates") | |
print('\n'.join(lprof_lines)) | |
print("---- Code ----") | |
print(source) | |
else: | |
print("\n".join(lprof_lines[:profile_start])) | |
print("\n".join(["{0} \t {1}".format(l, s) for l, s in zip(lprof_code_lines, source_lines)])) | |
return output_val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment