我可以获取线程完成所需的时间。如何获取线程消耗的内存。
import threading
import time
class mythread(threading.Thread):
def __init__(self,i,to):
threading.Thread.__init__(self)
self.h=i
self.t=to
self.st=0
self.end=0
def run(self):
self.st =time.time()
ls=[]
for i in range(self.t):
ls.append(i)
time.sleep(0.002)
self.end=time.time()
print "total time taken by {} is {}".format(self.h,self.end-self.st)
thread1=mythread("thread1",10)
thread2=mythread("thread2",20)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
cprofiler
并将其传递给kcachegrind
。如果您不知道我在说什么,可以阅读有关调试过程和内存分析的内容。 - Torxedtime.time()
所测量的是什么。它不是线程的CPU时间,因为time.time()
返回的是墙上时钟时间,即系统的实际时间。由于两个线程几乎同时启动,它们将同时执行,您基本上将测量它们的组合时间(以及在CPU上执行的任何其他内容)。也许您应该考虑使用time.thread_time()
? - JohanL