没有模块名为mem_profile
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)
我可以在这里使用任何替代软件包。请帮助。
使用此用于计算时间:
import time
time_start = time.clock()
#run your code
time_elapsed = (time.clock() - time_start)
作为由Python文档引用:
time.clock()
在Unix,返回当前处理器时间点数以秒为单位浮点数 。精确度,实际上“处理器时间”含义的定义取决于具有相同名称的C函数的定义,但在任何情况下,这都是 用于基准测试Python或定时算法的功能。
在Windows中,此函数返回由于 第一次调用该函数经过挂钟秒,作为一个浮点数,基于所述 Win32函数QueryPerformanceCounter的()。分辨率通常为 ,优于1微秒。
参考:http://docs.python.org/library/time.html
使用此计算内存:
import resource
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
参考:http://docs.python.org/library/resource.html
使用这个,如果你使用Python 3 .X:
画中画在安装过程中mem_profile它给了我错误无模块命名mem_profile。
默认情况下,pip会从PyPI下载软件包。在PyPI上没有名为“mem_profile”的软件包,所以当然你会得到一个错误。
定时的代码块中,timeit
模块是你想要的使用方法: https://docs.python.org/library/timeit.html
正在经历同样的教程,并遇到同样的问题。但经过进一步的研究,我发现本教程的作者使用了一个名为memory_profiler的包,其主文件已更改为mem_profile。他在代码教程中导入。
只要继续并做pip install memory_profiler。复制该文件并将其重命名为mem_profile.py,并将其保存在工作目录中。如果您在Windows上,请确保您安装依赖psutil包。
希望这有助于有人
尝试在代码块或者内联代码段中编写代码/命令 – martianwars
简单得多用SYS
import sys
...
print ('Memory (Before): {0}Mb'.format(sys.getsizeof([])))
即该模块手写(不Python包)。 我从Corey Schafer在他的YouTube视频中发表评论。 只是这段代码保存为模块的名称:
from pympler import summary, muppy
import psutil
import resource
import os
import sys
def memory_usage_psutil():
# return the memory usage in MB
process = psutil.Process(os.getpid())
mem = process.get_memory_info()[0]/float(2 ** 20)
return mem
def memory_usage_resource():
rusage_denom = 1024.
if sys.platform == 'darwin':
# ... it seems that in OSX the output is different units ...
rusage_denom = rusage_denom * rusage_denom
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/rusage_denom
return mem
我正好遇到了同样的问题。我解决了它安装memory_profiler
($ pip install -U memory_profiler
),并修改程序如下:
import memory_profiler
...
print('Memory (Before): {}Mb'.format(memory_profiler.memory_usage()))
添加到Adebayo Ibro的回答以上。执行以下操作:
- 在终端,运行
$ pip install memory_profiler
- 在脚本中,有
import memory_profiler as mem_profile
- 在脚本代替
import mem_profile
,与mem_profile.memory_usage()
取代所有mem_profile.memory_usage_resource()
。
希望这有助于!
1)首先导入模块
**pip install memory_profiler**
2)包括它在你的代码是这样
**import memory_profiler as mem_profile**
3)改变代码
mem_**profile.memory_usage_psutil()**
到**memory_usage()**
4)转换你打印这样的报表
**print('Memory (Before): ' + str(mem_profile.memory_usage()) + 'MB')**
**print('Memory (After) : ' + str(mem_profile.memory_usage()) + 'MB')**
**print ('Took ' + str(t2-t1) + ' Seconds')**
5),你将有这样的事情代码:
import memory_profiler as mem_profile
import random
import time
names = ['John', 'Corey', 'Adam', 'Steve', 'Rick', 'Thomas']
majors = ['Math', 'Engineering', 'CompSci', 'Arts', 'Business']
# print('Memory (Before): {}Mb '.format(mem_profile.memory_usage_psutil()))
print('Memory (Before): ' + str(mem_profile.memory_usage()) + 'MB')
def people_list(num_people):
result = []
for i in range(num_people):
person = {
'id': i,
'name': random.choice(names),
'major': random.choice(majors)
}
result.append(person)
return result
def people_generator(num_people):
for i in range(num_people):
person = {
'id': i,
'name': random.choice(names),
'major': random.choice(majors)
}
yield person
# t1 = time.clock()
# people = people_list(1000000)
# t2 = time.clock()
t1 = time.clock()
people = people_generator(1000000)
t2 = time.clock()
# print 'Memory (After) : {}Mb'.format(mem_profile.memory_usage_psutil())
print('Memory (After) : ' + str(mem_profile.memory_usage()) + 'MB')
# print 'Took {} Seconds'.format(t2-t1)
print ('Took ' + str(t2-t1) + ' Seconds')
现在,它做工精细读音字使用python 3.6和工作没有任何错误。
请勿使用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