Python question about time spent

I would like to know how much time a particular function spent on a program that includes recursion, what is the best way to do this?

thank

+3
source share
2 answers

The best way would be to perform some benchmarks (to test individual functions) or Profiling (to test the entire application / program). Python comes with built-in profilers.

, , , . .

:

import time
start = time.time()
do_long_code()
print "it took", time.time() - start, "seconds."

Python - , .

:

def test():
    "Time me"
    L = []
    for i in range(100):
        L.append(i)

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()
+11

!

python -m cProfile -o prof yourscript.py
runsnake prof

runsnake - . , .

: http://docs.python.org/library/profile.html

+3

Source: https://habr.com/ru/post/1752358/


All Articles