Iam使用此程序来测量两个功能所需的时间以及两个功能的内存要求,并比较哪一个最适合于使用大数据时的情况。但对于使用内存计算,我们需要mem_profile模块,但在pip install mem_profile
,它给了我错误No module named mem_profile
。没有模块名为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)
我可以在这里使用任何替代软件包。请帮助。
请勿使用time.clock()。这是误导,也被弃用。您引用的参考文献已过时,并且已在更新版本的python3文档中更新 –
您是在谈论[this](https://docs.python.org/3/library/time.html#time.clock)。 @CoreyGoldberg – Devansh
我没有太多的Python经验,截至目前我正在使用Python 2.7,所以我找到了解决方案。我同意你@CoreyGoldberg使用Python 3.x. – Devansh