我正在使用这个程序来测量两个函数使用的时间和内存,并比较哪个更适合处理大量数据。我的理解是,要测量内存使用情况,我们需要 mem_profile
模块,但在 pip install mem_profile
期间它给了我错误 No module named mem_profile
import mem_profile
import random
import time
names = ['Kiran','King','John','Corey']
majors = ['Math','Comps','Science']
print 'Memory (Before): {}Mb'.format(mem_profile.memory_usage_resource())
def people_list(num_people):
results = []
for i in num_people:
person = {
'id':i,
'name': random.choice(names),
'major':random.choice(majors)
}
results.append(person)
return results
def people_generator(num_people):
for i in xrange(num_people):
person = {
'id':i,
'name': random.choice(names),
'major':random.choice(majors)
}
yield person
t1 = time.clock()
people = people_list(10000000)
t2 = time.clock()
# t1 = time.clock()
# people = people_generator(10000000)
# t2 = time.clock()
print 'Memory (After): {}Mb'.format(mem_profile.memory_usage_resource())
print 'Took {} Seconds'.format(t2-t1)
是什么导致了这个错误?有没有我可以使用的替代包?
原文由 SaiKiran 发布,翻译遵循 CC BY-SA 4.0 许可协议
用它来计算时间:
正如 Python 文档所引用的:
参考: https ://docs.python.org/3/library/time.html#time.time
用它来计算内存:
参考: http ://docs.python.org/library/resource.html
如果你使用 python 3.x,请使用它:
参考: https ://docs.python.org/3/library/timeit.html