测试python awk sed 读取文件指定位置时的性能
2015-05-30 21:19
543 查看
#!/bin/env python#coding:utf8'''awk 打印指定行数sed 打印指定行数python 打印指定位置,某长度字符串awk 耗时最长,很长sed awk 时间一半python 耗时 基本忽略不计使用脚本监控日志文件的时候,每次记录上次退出的位置python效率最高.'''import osfrom time import timefrom os.path import getsizetestfile='/dev/shm/%s' % time()#生成文件总行数 10**8 大约800M 10**7 大约 80Mlinesize=10**7#提取文件中间行halfline=int(linesize / 2)#文件大小一半halffilesize=0#测试次数num=10#生成文件大小filesize=0r=[]def create_testfile():f=open(testfile,'w')for i in xrange(linesize):f.write(str(i)+'\n')f.close()def time1(func):t1=time()func()t2=time()t=t2-t1print func.func_name,tr.append("%s:\t\t%s" % (func.func_name,str(t) ) )def awk():for i in range(num):os.system("/bin/awk 'NR==%s { print $0 }' %s " % (halfline,testfile) )def sed():for i in range(num):os.system("/bin/sed -n %sp %s " % (halfline,testfile) )#os.system("/bin/sed -n %sp %s|awk '{print $0}' " % (halfline,testfile) )def py():for i in range(num):fn=open(testfile)#读取fn.seek(halffilesize)print fn.read(7)fn.close()print "create test file!"create_testfile()filesize=getsize(testfile)halffilesize=int(filesize/2)time1(awk)time1(sed)time1(py)print "\n\n"print "halffilesize:\t\t",halffilesizeprint "filesize:\t\t",filesizeos.system('/bin/ls -lh %s' % testfile)print "\t"for i in r:print ios.unlink(testfile)
相关文章推荐
- Python Template 错误
- 基于python的机器学习(一)
- Python2.7-异常和工具
- python学习笔记第一课_Phoenix-晶
- [笔记]python的os库
- Effective Python 条目二:遵循PEP8风格规约
- Python 中有关中文编码解码小记
- 在Python的Django框架下使用django-tagging的教程
- python 3 最简单的小爬虫
- 使用url_helper简化Python中Django框架的url配置教程
- 在Python的Django框架中simple-todo工具的简单使用
- <Python基础教程>_第四章_总结
- Python爬虫爬取网页图片
- Python中Django框架下的staticfiles使用简介
- Python爬虫爬取贴吧的帖子内容
- Python fnmatch 模块
- 简单介绍Python的Django框架的dj-scaffold项目
- Python的Django框架中settings文件的部署建议
- 排序算法(python实现)
- Python核心编程第九章笔记及习题记录[待更新]