日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python awk 读文件_测试python awk sed 读取文件指定位置时的性能

發布時間:2025/3/20 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python awk 读文件_测试python awk sed 读取文件指定位置时的性能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#!/bin/env python

#coding:utf8

'''

awk 打印指定行數

sed 打印指定行數

python 打印指定位置,某長度字符串

awk 耗時最長,很長

sed awk 時間一半

python 耗時 基本忽略不計

使用腳本監控日志文件的時候,每次記錄上次退出的位置

python效率最高.

'''

import os

from ?time import time

from os.path import getsize

testfile='/dev/shm/%s' % time()

#生成文件總行數 10**8 大約800M ?10**7 大約 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 $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",halffilesize

print "filesize:\t\t",filesize

os.system('/bin/ls -lh %s' % testfile)

print "\t"

for i in r:

print i

os.unlink(testfile)

總結

以上是生活随笔為你收集整理的python awk 读文件_测试python awk sed 读取文件指定位置时的性能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。