Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
28cbc819
提交
28cbc819
authored
8月 22, 2012
作者:
Steven Pigeon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed pep8 issues
上级
e43983b8
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
20 行增加
和
19 行删除
+20
-19
python-memory-management.rst
doc/tutorial/python-memory-management.rst
+20
-19
没有找到文件。
doc/tutorial/python-memory-management.rst
浏览文件 @
28cbc819
...
...
@@ -31,17 +31,17 @@ if necessary):
import sys
def show_sizeof(x,level=0):
def show_sizeof(x,
level=0):
print "\t"*level,x.__class__, sys.getsizeof(x), x
print "\t"*level,
x.__class__, sys.getsizeof(x), x
if hasattr(x,'__iter__'):
if hasattr(x,'items'):
if hasattr(x,
'__iter__'):
if hasattr(x,
'items'):
for xx in x.items():
show_sizeof(xx,
level+
1)
show_sizeof(xx,
level +
1)
else:
for xx in x:
show_sizeof(xx,
level+
1)
show_sizeof(xx,
level +
1)
We can now use the function to inspect the sizes of the different basic
data types:
...
...
@@ -139,7 +139,7 @@ heterogeneous. Let us look at our sizes:
::
show_sizeof([])
show_sizeof([4,
"toaster",230.1,
])
show_sizeof([4,
"toaster", 230.1
])
outputs
...
...
@@ -162,7 +162,7 @@ about tuples? (and dictionaries?):
::
show_sizeof({})
show_sizeof({'a':213,'b':2131})
show_sizeof({'a':213,
'b':2131})
outputs, on a 32-bits box
...
...
@@ -288,11 +288,11 @@ program (it makes my point entirely):
::
port copy, memory_profiler
im
port copy, memory_profiler
@profile
def function():
x=range(1000000) # allocate a big list
x=range(1000000)
# allocate a big list
y=copy.deepcopy(x)
del x
return y
...
...
@@ -428,23 +428,23 @@ A naïve implementation would give:
import memory_profiler, random, pickle
def random_string():
return "".join([ chr(64+random.randint(0,25)) for _ in xrange(20) ])
return "".join([ chr(64+random.randint(0,
25)) for _ in xrange(20) ])
@profile
def create_file():
x=[ (random.random(),
random_string(),
random.randint(0,2**64))
random.randint(0,
2**64))
for _ in xrange(1000000) ]
f=open('machin.flat','w')
f=open('machin.flat',
'w')
for xx in x:
print >>f, xx
@profile
def load_file():
y=[]
f=open('machin.flat','r')
f=open('machin.flat',
'r')
for line in f:
y.append(eval(line))
return y
...
...
@@ -465,10 +465,10 @@ Creating the file:
9 9.19 MB 0.00 MB def create_file():
10 9.34 MB 0.15 MB x=[ (random.random(),
11 random_string(),
12 random.randint(0,2**64))
12 random.randint(0,
2**64))
13 246.09 MB 236.75 MB for _ in xrange(1000000) ]
14
15 246.09 MB 0.00 MB f=open('machin.flat','w')
15 246.09 MB 0.00 MB f=open('machin.flat',
'w')
16 308.27 MB 62.18 MB for xx in x:
17 print >>f, xx
...
...
@@ -483,7 +483,7 @@ and reading the file back:
20 @profile
21 9.19 MB 0.00 MB def load_file():
22 9.34 MB 0.15 MB y=[]
23 9.34 MB 0.00 MB f=open('machin.flat','r')
23 9.34 MB 0.00 MB f=open('machin.flat',
'r')
24 300.99 MB 291.66 MB for line in f:
25 300.99 MB 0.00 MB y.append(eval(line))
26 301.00 MB 0.00 MB return y
...
...
@@ -501,8 +501,9 @@ whole data. Using pickle, you would allocate the whole data (at least)
twice: once by pickle, and once through Numpy.
Or even better yet: use Numpy (or PyTables) arrays. But that's a different
topic that is discussed in 'loading and saving' another tutorial in the
Theano/doc/tutorial directory.
topic. In the mean time, you can have a look at `loading and saving
<https://github.com/Theano/Theano/blob/master/doc/tutorial/loading_and_saving.txt>`_
another tutorial in the Theano/doc/tutorial directory.
\*
\* \*
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论