How do I profile memory usage in Python?

前端 未结 8 551
无人及你
无人及你 2020-11-22 09:50

I\'ve recently become interested in algorithms and have begun exploring them by writing a naive implementation and then optimizing it in various ways.

I\'m already f

8条回答
  •  囚心锁ツ
    2020-11-22 10:15

    maybe it help:

    pip install gprof2dot
    sudo apt-get install graphviz
    
    gprof2dot -f pstats profile_for_func1_001 | dot -Tpng -o profile.png
    
    def profileit(name):
        """
        @profileit("profile_for_func1_001")
        """
        def inner(func):
            def wrapper(*args, **kwargs):
                prof = cProfile.Profile()
                retval = prof.runcall(func, *args, **kwargs)
                # Note use of name from outer scope
                prof.dump_stats(name)
                return retval
            return wrapper
        return inner
    
    @profileit("profile_for_func1_001")
    def func1(...)
    

提交回复
热议问题