日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/20 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 读取文件指定位置时的性能的全部內容,希望文章能夠幫你解決所遇到的問題。

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