#!/bin/env python
#coding: UTF8
‘‘‘
awk prints the specified number of rows
SED prints the specified number of lines
Python prints the specified position, a length string
Awk takes the longest, a long time
Sed awk time Half
Python time-consuming basic negligible
When you use a script to monitor log files, the location of the last exit is logged
Python is the most efficient.
‘‘‘
Import OS
From time Import time
From Os.path import GetSize
testfile= '/dev/shm/%s '% time ()
#生成文件总行数 10**8 approx. 800M 10**7 approx. 80M
Linesize=10**7
#提取文件中间行
Halfline=int (LINESIZE/2)
#文件大小一半
Halffilesize=0
#测试次数
num=10
#生成文件大小
Filesize=0
R=[]
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-t1
Print Func.func_name,t
R.append ("%s:\t\t%s"% (FUNC.FUNC_NAME,STR (t)))
Def awk ():
For I in range (num):
Os.system ("/bin/awk ' nr==%s {print $} '%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 $} '"% (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"
Print "halffilesize:\t\t", halffilesize
Print "filesize:\t\t", FileSize
Os.system ('/bin/ls-lh%s '% testfile)
print "\ T"
For I in R:
Print I
Os.unlink (testfile)
Testing the performance of Python awk sed when reading a file at a specified location