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.
|
|