2016-12-16 158 views
1

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) 

我可以在這裏使用任何替代軟件包。請幫助。

回答

2

使用此用於計算時間:

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:

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

+0

請勿使用time.clock()。這是誤導,也被棄用。您引用的參考文獻已過時,並且已在更新版本的python3文檔中更新 –

+0

您是在談論[this](https://docs.python.org/3/library/time.html#time.clock)。 @CoreyGoldberg – Devansh

+0

我沒有太多的Python經驗,截至目前我正在使用Python 2.7,所以我找到了解決方案。我同意你@CoreyGoldberg使用Python 3.x. – Devansh

0

畫中畫在安裝過程中mem_profile它給了我錯誤無模塊命名mem_profile。

默認情況下,pip會從PyPI下載軟件包。在PyPI上沒有名爲「mem_profile」的軟件包,所以當然你會得到一個錯誤。


定時的代碼塊中,timeit模塊是你想要的使用方法: https://docs.python.org/library/timeit.html

4

正在經歷同樣的教程,並遇到同樣的問題。但經過進一步的研究,我發現本教程的作者使用了一個名爲memory_profiler的包,其主文件已更改爲mem_profile。他在代碼教程中導入。

只要繼續並做pip install memory_profiler。複製該文件並將其重命名爲mem_profile.py,並將其保存在工作目錄中。如果您在Windows上,請確保您安裝依賴psutil包。

希望這有助於有人

+0

嘗試在代碼塊或者內聯代碼段中編寫代碼/命令 – martianwars

0

簡單得多用SYS

import sys 
    ... 
    print ('Memory (Before): {0}Mb'.format(sys.getsizeof([]))) 
1

即該模塊手寫(不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 
1

我正好遇到了同樣的問題。我解決了它安裝memory_profiler$ pip install -U memory_profiler),並修改程序如下:

import memory_profiler 
... 
print('Memory (Before): {}Mb'.format(memory_profiler.memory_usage())) 
1

添加到Adebayo Ibro的回答以上。執行以下操作:

  • 在終端,運行$ pip install memory_profiler
  • 在腳本中,有import memory_profiler as mem_profile
  • 在腳本代替import mem_profile,與mem_profile.memory_usage()取代所有mem_profile.memory_usage_resource()

希望這有助於!

0

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和工作沒有任何錯誤。

相關問題