Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PyCharm profiler show only timings of my source code, not any libraries?

When I run PyCharm profiler (a quick intro video is here -- https://www.youtube.com/watch?v=QSueV8MYtlw ) I get thousands of lines like hasattr or npyio.py (where did it come from, I do not even use numpy) which do not help me to understand what's going on at all.

How can I make make PyCharm profiler to show only timings of my source code, not any libraries or system calls?

In other words, can the time spent in system calls and libraries be assigned to my functions which call them?

In other words (version two), all I want is number of milliseconds next to each line of my python code, not anything else.

like image 936
Castle Avatar asked Aug 30 '18 10:08

Castle


1 Answers

I created a code to provide an example and hopefully provide an acceptable answer:

import datetime as dt
class something:
    def something_else(self):
        other_list = range(100000)
        for num in other_list:
            datetimeobj = dt.datetime.fromtimestamp(num)
            print(num)
            print(datetimeobj)

    def something_different(self):
        other_list = range(100000)
        for num in other_list:
            datetimeobj = dt.datetime.fromtimestamp(num)
            print(num)
            print(datetimeobj)

st = something()
st.something_else()
st.something_different()

The code resulted in the below picture, which I have sorted based on name. (This is in my case possible because all of the builtin methods are prefixed by "<". After doing this I can now see that main took 100 % of the total time (Column: Time (ms)). something_else took 50.8 % of the time and something_different took 49.2 % of the time (Totaling to 100 % as well) (Column: Time (ms)) The time spent inside each of the two home-grown methods was 2.0 % for each (Column: Own Time (ms)) -> This means that underlying calls from something_else accounted for 48.8 % and for something_different 47.2 % and the parts that I wrote accounted for 4.0 % of the total time. The remaining 96.0 % of the code happens from the built-in methods, that I call.

Result

Your questions were:

How can I make make PyCharm profiler to show only timings of my source code, not any libraries or system calls? -> That's what you see in the column: "Own Time (ms)" -> 2.0 % (Time spent inside the specific method.)

In other words, can the time spent in system calls and libraries be assigned to my functions which call them? -> That's what you see in the column: "Time (ms)" (Time spent including underlying methods.)

Subtract the two columns and you get time spent only in underlying methods.

I have unfortunately been unable to find a method for filtering in the profiler, but it is possible to export the list by copying it and this way you could create something else to do the filtering on e.g. "<built_in" to clean up the data.

like image 61
Phorys Avatar answered Nov 16 '22 16:11

Phorys