【发布时间】:2018-11-10 18:59:43
【问题描述】:
在这里我可以得到线程完成的时间。如何获取线程消耗的内存。
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。如果您不知道我在说什么,请read up 了解调试过程和内存分析。 -
我想知道你是否知道你测量的是什么,使用
time.time()。这不是线程的 cpu 时间,因为time.time()返回的是挂钟时间,即系统的实际时间。由于两个线程或多或少是并行启动的,它们将同时执行,并且您基本上将测量它们的组合时间(以及当时在 CPU 上发生的任何其他事情)。也许您想改为查看time.thread_time()?
标签: python multithreading