0

I have an existing library and I want to optimize the imports in order to improve load time.

Using the cProfile it doesn't really show much. Is there a different profiler to examine the imports of a library?

Thank you

JabberJabber
  • 341
  • 2
  • 17

1 Answers1

0

Very interesting question.

This would be an excellent case where a statistical profiler just won't work, as there could never be enough samples to show useful data without introducing a huge overhead.

I think viztracer is what you are looking for. It can visualize the calls when you import a library.

import tqdm

Let's say you have a test.py file above

pip install viztracer
viztracer test.py
vizviewer result.json

enter image description here

The report will be like this. You can investigate more about what's slow.

There is a pivot though, as VizTracer imported some built-in libraries itself, it will have an impact on the speed of your library importing, to be more specific, making your import faster as some of the modules have been loaded.

However, I still think this would be a much better tool than cProfile, because cProfile has this issue as well.

minker
  • 510
  • 6
  • 3