Testing the performance of Python awk sed when reading a file at a specified location

Source: Internet
Author: User


#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.