2007年01月22日 星期一 20:22
从一两百万个文件中过滤出几十万个特定ID的行,
希望能提高操作速度,各位有好方法吗?
假定:1.特定id放在id.txt文件
2.要过滤的文件的首行、尾行[21:28]是记录总数,一个文件最多有9999999个记录。
3.记录位于首行、尾行之间,一行代表一个记录,每行[13:37]是记录的ID,使用前需去掉两头空格
4.遍历每个文件,匹配的行记录(记录的ID若在id.txt文件中)写入一个新文件,文件名相同。
5.要过滤的文件平均分布在5个文件目录(也许可以根据实际调整成更多的目录),传入不同的目录参数即可并行处理。
参考代码:
省略了部分写文件、以及程序重新执行后做检查的代码。
经测试,
环境:HP-UX bilut21 B.11.11 U 9000/800 (ti),系统有2G内存及足够的硬盘空间
python 版本 :python2.3
要过滤的id数 :350000
所有文件记录数: 29258423
匹配记录数 : 4393239
花费时间(秒) : 913
测试文件数 : 11248
----------------------------------------------------------
import os,mmap,time
inputdir='./data/input/'
outputdir='./data/output/'
idfile='./data/id.txt'
#右对齐
def rjust(s,width,char):
if len(s)>=width :
return s[0:width]
l = list(s)
for i in range(width-len(s)) :
l.insert(0,char)
return ''.join(l)
if __name__ == '__main__':
#缓存要过滤的ID
ids = {}
f = open(idfile,'r')
f.seek(0,2)
m = mmap.mmap(f.fileno(),f.tell(),access=mmap.ACCESS_READ)
while True:
line = m.readline().strip()
if len(line) <= 0 :
break;
ids[line] = None
m.close()
f.close()
#------------------------------------------------------------
#遍历目录,过滤文件
inputfileList = os.listdir(inputdir)
filterlist = []
for filename in inputfileList :
starttime = time.time()
print filename
#check if retry
# ....
#Memory-mapped file
f = open('%s%s'%(inputdir,filename),'r')
f.seek(0,2)
m = mmap.mmap(f.fileno(),f.tell(),access=mmap.ACCESS_READ)
#取文件记录数
headline = m.readline()
numRecords = int(headline[21:28])#numRecords 一定大于0
lineno = 1
while True:
line = m.readline();
if lineno >= numRecords + 1 :
trailline=line
break
else :
exid = line[13:37].strip()
if ids.has_key(exid) :
filterlist.append(line)
lineno = lineno + 1
m.close()
f.close()
#根据filterlist生成新文件,也需要花费时间
#...
#reset
filterlist = []
print "process file time:",(time.time()-starttime)
print "#the end"
2007年01月22日 星期一 21:02
建议先用 hotshot 模块看看瓶颈在哪里。 另外,如果不用 mmap,直接使用 f.readlines() 把文件内容读到内存中,这样就不用判断 是不是最后一行了,省了判断的时间,是不是会快点,还是差不多? yemin wrote:: > 从一两百万个文件中过滤出几十万个特定ID的行, > 希望能提高操作速度,各位有好方法吗? -- Vim 中文 Google 论坛 http://groups.google.com/group/Vim-cn
2007年01月23日 星期二 09:18
用f.xreadlines可能性能更高一些,对于这种数据很大的情况 tocer 写道: > 建议先用 hotshot 模块看看瓶颈在哪里。 > > 另外,如果不用 mmap,直接使用 f.readlines() 把文件内容读到内存中,这样就不用判断 > 是不是最后一行了,省了判断的时间,是不是会快点,还是差不多? > > yemin wrote:: > >> 从一两百万个文件中过滤出几十万个特定ID的行, >> 希望能提高操作速度,各位有好方法吗? >> > >
2007年01月23日 星期二 09:27
On 1/23/07, amingsc <amingsc在gmail.com> wrote: > 用f.xreadlines可能性能更高一些,对于这种数据很大的情况 > 性能方面就不要在Python 这边儿找方法了, 直接使用 find 命令要高效的多, 可以使用Python 来辅助生成命令,直接利用系统工具来进行过滤, 这样开发和执行效率都可以保证了 > tocer 写道: > > 建议先用 hotshot 模块看看瓶颈在哪里。 > > > > 另外,如果不用 mmap,直接使用 f.readlines() 把文件内容读到内存中,这样就不用判断 > > 是不是最后一行了,省了判断的时间,是不是会快点,还是差不多? > > > > yemin wrote:: > > > >> 从一两百万个文件中过滤出几十万个特定ID的行, > >> 希望能提高操作速度,各位有好方法吗? > >> > > > > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- '''Time is unimportant, only life important! blog@ http://blog.zoomquiet.org/pyblosxom/ wiki@ http://wiki.woodpecker.org.cn/moin/ZoomQuiet douban@ http://www.douban.com/people/zoomq/ ____________________________________ Please use OpenOffice.org to replace M$ office. http://zh.openoffice.org Please use 7-zip to replace WinRAR/WinZip. http://7-zip.org/zh-cn/ You can get the truely Freedom from software. '''
2007年01月23日 星期二 13:27
同意,一些unix下的工具对于文本处理有极高的效率。而且楼主的处理量不小,我觉得应该设计一个并发的算法。也不难,就是一个文件给一个线程去做,共享那个存放ID的集合。另外,楼主是用字典来存id的,如果id是数字的话,有没有试过用列表存,排序然后用二分查找是不是能快点? On 1/23/07, Zoom. Quiet <zoom.quiet在gmail.com> wrote: > > On 1/23/07, amingsc <amingsc在gmail.com> wrote: > > 用f.xreadlines可能性能更高一些,对于这种数据很大的情况 > > > 性能方面就不要在Python 这边儿找方法了, > 直接使用 find 命令要高效的多, > > 可以使用Python 来辅助生成命令,直接利用系统工具来进行过滤, > 这样开发和执行效率都可以保证了 > > > tocer 写道: > > > 建议先用 hotshot 模块看看瓶颈在哪里。 > > > > > > 另外,如果不用 mmap,直接使用 f.readlines() 把文件内容读到内存中,这样就不用判断 > > > 是不是最后一行了,省了判断的时间,是不是会快点,还是差不多? > > > > > > yemin wrote:: > > > > > >> 从一两百万个文件中过滤出几十万个特定ID的行, > > >> 希望能提高操作速度,各位有好方法吗? > > >> > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > -- > '''Time is unimportant, only life important! > blog@ http://blog.zoomquiet.org/pyblosxom/ > wiki@ http://wiki.woodpecker.org.cn/moin/ZoomQuiet > douban@ http://www.douban.com/people/zoomq/ > ____________________________________ > Please use OpenOffice.org to replace M$ office. > http://zh.openoffice.org > Please use 7-zip to replace WinRAR/WinZip. > http://7-zip.org/zh-cn/ > You can get the truely Freedom from software. > ''' > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- Best Regards, Archer Ming Zhe Huang -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20070123/5ac8b136/attachment.html
Zeuux © 2025
京ICP备05028076号