没有名为mem_profile的模块

Sai*_*ran 2 python pip list generator python-3.x

我使用这个程序来测量两个函数所花费的时间以及两个函数的内存需求,并比较哪些函数在使用大数据时最适合于情境.但是对于使用内存计算,我们需要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)
Run Code Online (Sandbox Code Playgroud)

我可以在这里使用任何替代包.请帮助.

H S*_*ooq 10

1)第一个导入模块

pip install memory_profiler
Run Code Online (Sandbox Code Playgroud)

2)将它包含在你的代码中

import memory_profiler as mem_profile
Run Code Online (Sandbox Code Playgroud)

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')
Run Code Online (Sandbox Code Playgroud)

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')
Run Code Online (Sandbox Code Playgroud)

现在它使用python 3.6工作正常,它的工作没有任何错误.


小智 9

正在经历相同的教程并遇到了同样的问题.但经过进一步的研究,我发现本教程的作者使用了一个名为memory_profiler的软件包,他将主文件更改为mem_profile.他在代码教程中导入的.

请继续并执行pip install memory_profiler.将文件复制并重命名为工作目录中的mem_profile.py,您应该没问题.如果您使用的是Windows,请确保同时安装依赖的psutil软件包.

希望这有助于某人


Dev*_*nsh 6

使用它来计算时间:

import time

time_start = time.time()
#run your code
time_elapsed = (time.time() - time_start)
Run Code Online (Sandbox Code Playgroud)

正如 Python 文档所引用的:

时间.时间()?float 以浮点数形式返回自纪元以来的时间(以秒为单位)。纪元的具体日期和闰秒的处理取决于平台。在 Windows 和大多数 Unix 系统上,纪元是 1970 年 1 月 1 日 00:00:00 (UTC),闰秒不计入纪元以来的时间(以秒为单位)。这通常称为 Unix 时间。要找出给定平台上的纪元,请查看 gmtime(0)。

请注意,即使时间总是作为浮点数返回,但并非所有系统都提供比 1 秒更精确的时间。虽然此函数通常返回非递减值,但如果系统时钟在两次调用之间已回退,则它可以返回比前一次调用低的值。

通过将 time() 返回的数字传递给 gmtime() 函数,可以将 time() 返回的数字转换为更常见的 UTC 时间格式(即年、月、日、小时等),或者通过将其传递给 localtime() ) 功能。在这两种情况下,都会返回一个 struct_time 对象,可以从中访问日历日期的组件作为属性。

参考https : //docs.python.org/3/library/time.html#time.time


使用它来计算内存:

import resource

resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
Run Code Online (Sandbox Code Playgroud)

参考 http //docs.python.org/library/resource.html

如果您使用 python 3.x,请使用它:

参考https : //docs.python.org/3/library/timeit.html

  • 不要使用 time.clock()。它具有误导性,也已被弃用。您引用的参考已过时,已在较新版本的 python3 文档中更新 (2认同)
  • 就是那个。time.clock() 在 Windows 和其他平台上也有不同的行为。我建议删除这个答案 (2认同)

SKR*_*lan 5

以上是Adebayo Ibro的答案。请执行下列操作 :

  • 在终端中,运行 $ pip install memory_profiler
  • 在脚本中,替换import mem_profileimport memory_profiler as mem_profile
  • 在脚本中,将全部 替换mem_profile.memory_usage_resource()mem_profile.memory_usage()

希望这可以帮助!