由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这个python code能否优化去掉file write
相关主题
包子,能否在Python 里生成一个csv 文件,并将它放在一个server 的directory 下?python programming question
没人觉得python的string是immutable不爽吗?请教一个python的概念问题
Re: [转载] how would you do this?一个popen加gzip的问题
streamwriter and filestream《Objective-C 编程 (第3版)》英文文字版[PDF]
golang 一个thread safe singleton问题RODBC and BLOB
How to initialize object in constructor?python smtp 587 连不上gmail, socket error?
问个Python的问题再 次 请 教 : 关 于 writing to a file 用 Perl for CGI
a python question[合集] some file operation questions
相关话题的讨论汇总
话题: __话题: file话题: object话题: def话题: log
进入Programming版参与讨论
1 (共1页)
e*********6
发帖数: 3453
1
class C:
def __init__(self, x):
self.x = x
def write_to_file(object, filename):
f = open(filename, 'a')
f.write(str(object.x) + 'n')
def large_loop(object):
print(object.x)
if (object.x == 1000):
print ('finish')
else:
n = object.x + 1
new_object = C(n)
if (object.x % 100 == 0):
# we need record some information
write_to_file(object, "temp.log")
return large_loop(new_object)
def working_on_intermidiate(filename):
#read in the log file and do some other work and delete log file
if __name__ == "__main__":
o1 = C(50)
large_loop(o1)
对一个大程序,我需要记录一些中间结果,然后下一步进一步处理,上边的code是把这些
中间结果写到文件里然后再次读出来处理,这样肯定会影响效率,硬盘的读写速度比内存
或者cache慢.现在有两个问题: 1, 这样对效率的影响到底有多大? 如果真的要写 硬盘
,那可能好几个page都要进进出出,CPU肯定就要先去干其他的去了.就算写内存, 也要好
多block搬进搬出cache, 到底对运行时间影响多大? 2, 如何才能改进?能否由办法分配
一块空间专门储存这个log,现在相当于是通过写文件分配和一个固定的地方存log,但是
能否直接在内存之上的级别做了
p******g
发帖数: 347
2
check out StringIO/cStringIO for file-like memory buffer.

【在 e*********6 的大作中提到】
: class C:
: def __init__(self, x):
: self.x = x
: def write_to_file(object, filename):
: f = open(filename, 'a')
: f.write(str(object.x) + 'n')
: def large_loop(object):
: print(object.x)
: if (object.x == 1000):
: print ('finish')

p***o
发帖数: 1252
3
每次写之前都把文件打开一次,神仙也救不了啊。

【在 p******g 的大作中提到】
: check out StringIO/cStringIO for file-like memory buffer.
1 (共1页)
进入Programming版参与讨论
相关主题
[合集] some file operation questionsgolang 一个thread safe singleton问题
怎么用bash在for loop里给文件名用counter?How to initialize object in constructor?
Python里边file writer的问题问个Python的问题
初级和中高级的C 语言教程有什么区别?a python question
包子,能否在Python 里生成一个csv 文件,并将它放在一个server 的directory 下?python programming question
没人觉得python的string是immutable不爽吗?请教一个python的概念问题
Re: [转载] how would you do this?一个popen加gzip的问题
streamwriter and filestream《Objective-C 编程 (第3版)》英文文字版[PDF]
相关话题的讨论汇总
话题: __话题: file话题: object话题: def话题: log