While processing data for training models or trying to increase inference speed, I often face slow code that I need to fix. I tried most python profilers and I really like VizTracer because it’s very easy to use and very powerful as you can zoom in and out on your code execution timeline.
To use it, you simply have to import a "tracer" and use the start, stop and save methods like that :

It will save a profile.json file that contains your code execution timeline. You then have to run the command (vizviewer is installed when you install viztracer)

The zoom/navigation in your code execution timeline is possible using the keys AWSD which makes it very easy and fast. You can also use the mouse. The video below shows an example of how to navigate through the profile.json we created. It’s powered by Perfetto (made by Google) which is optimized to handle even huge traces files (multiple GBs).
I zoomed in using W, zoomed out using S and move left/right with A/D. You can see the main function (outer_function) at the top and then inner_function right below it, and then the time.sleep calls. By clicking on one of those rectangles you can see more details about this function call (duration, start/end time, etc.).
By clicking and holding, you can select a portion of the code execution timeline and get a summary. You’ll get the function that has been the longest, how many times it was called, etc.
Track variables
You can also track variables (assuming you’re Debugging) using the VizCounter class. For example, we’ll flip a coin and follow the number of heads, tails, and the proportion of heads using this code :

which, through the "vizviewer profile2.json" command, produces :

I’ve shown only a small set of features of [VizTracer](https://github.com/gaogaotiantian/viztracer). You can track variables, profile multiprocessing/multithreaded programs, function’s entries, etc. See VizTracer for more information. The author is also on medium https://gaogaotiantian.medium.com/