import time
import cProfile
import pstats
def add(x, y):
resulting_sum = 0
resulting_sum += x
resulting_sum += y
return resulting_sum
def fact(n):
result = 1
for i in range(1, n+1):
result *= i
return result
def do_stuff():
result = []
for x in range(10000000):
result.append(x**2)
return result
def waste_time():
time.sleep(5)
print("Hello")
if __name__ == "__main__":
with cProfile.Profile() as profile:
print(add(100, 5000))
print(fact(70))
print(do_stuff())
waste_time()
results = pstats.Stats(profile)
results.sort_stats(pstats.SortKey.TIME)
results.print_stats()
results.dump_stats("results.prof")
结果如下:
我们可以看到,这个程序中最耗时的函数是waste_time(),用了约5s。其次是do_stuff(),用了3.8s。