foreign
here for infoworld in this episode of smart python I'm going to take a look at line profiler
a profiling utility for python it lets us see what specific lines of code are taking up the
most time in your program now in an earlier video I looked at C profile which is a module
in the standard library for profiling python code on a function call by function call basis
and it is a powerful tool but it isn't always able to give you the granularity you need to find out
what is slow sometimes the slowness in a given function can revolve around a particular line
of code and not the function as a whole this is where line profiler comes in it's a third party
utility not part of the standard library but it's maintained on GitHub and you can install
it from Pi Pi using pip install line profiler so for an example of it at work I'm going to
use a script I demonstrated in an earlier video the Google engram Benchmark to find out which
specific lines of this code are the slowest so once I've installed line profiler I'm going to
do two things to profile this code first I add a decorator profile to the function I want to
analyze note that our linter is flagging this as not having an import which is deliberate
the actual analysis is going to be handled externally and I'll show you what I mean by that
so to run the actual analysis I'll use this command to kick offline profiler current Prof what
this is is a helper script it lets me use LINE profiler without having to make large changes to
my original program as you saw all I did was add a decorator to the function that I want analyzed
so when I run my script through current Prof it's going to take much longer to complete than usual
because of the overhead created by line profiler now unfortunately this is not something we can
do very much about except maybe by confining our profiling to a function that we suspect is a hot
spot in the code so I'm going to magically skip to the end of the run and show you the results
now the report that is generated here gives us each line of the function that we decorated
along with timing information about each line the column we most want to pay attention to is the
percentile column this shows us very quickly which lines take up the biggest chunk of the time in
the function run and as you can see here what are the big culprits is this try accept statement see
what's interesting is that the accept statement never runs because we have no percentages listed
for it that tells us that we are wasting our time wrapping this line in a try block to begin with
so we can get back some 12 to 13 of our run time by simply getting rid of the try accept statement
so now I'm going to rerun this Pro same profile and I'm going to Output the results to a new
file that we can inspect and again because of the amount of time this is going to take I'm
just going to cut to the results now as you can see even with the overhead from the profiler we
have shaved a fair amount of time off the run for this application and that's all because we
discovered with line profiling that we had a totally Superfluous triceps Block in our code
now for comparison's sake here's an example of how we can use LINE profiler a little more explicitly
without having to edit our original code at all so we recreate a new script and we import into it
line profiler and we create a decorator object LP and then we import the main function from
our code and then we wrap that so after we call the code we then use the print stats method for
The Decorator to write statistics out to the console or to write to a file this approach
here may be better for you for testing because you don't actually have to make any changes to
Source you can just create a new file import your existing code and work with that so to sum up line
profiler lets you analyze your code line by line to find out where it has its largest bottlenecks
profiling doesn't pose a significant cost on the code's runtime so you want to use it as
selectively as you can but you don't need to use it in production you only use it for testing and
you can also profile your code non-destructively with line profiler and tease out performance
issues with it you might not find otherwise so that's it for this video if you liked it please
leave a comment below don't forget to subscribe to the tech talk Channel on YouTube and for more
smart Python and more Dev with certar be sure to follow me on Facebook YouTube and infoworld.com