python - 没有名为 mem_profile 的模块

标签 python list python-3.x pip generator

我正在使用这个程序来测量两个函数使用的时间和内存,并比较哪个函数更适合处理大量数据。我的理解是,要测量内存使用情况,我们需要 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)

是什么导致了这个错误?有没有我可以使用的替代包?

最佳答案

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 时运行良好并且没有任何错误。

关于python - 没有名为 mem_profile 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41191412/

相关文章:

python - 如何优化 Python 程序

python - 如何将二维列表转换为集合?

python - 导入 django_filters.rest_framework 错误

sql - T-SQL,制作列表

c++ - std::remove_if 中的 const 参数

python - 如何用Python制作一个像C++一样的计算器

python-3.x - 使用 PostgreSQL copy_from、psycopg2 和 StringIO 批量插入

python - 在 Python 中将 1 加到 16 字节的数字

python - pygtk 窗口大小调整中的意外行为

list - 使用流的 SML 惰性排序 int 列表