python如何获取文件行数_如何高效地获取文件行数

简单的做法:

需要在python中获取大文件(数十万行)的行数。def file_len(fname):

with open(fname) as f:

for i, l in enumerate(f):

pass return i + 1

有效的方法(缓冲区读取策略):

首先看下运行的结果:mapcount : 0.471799945831

simplecount : 0.634400033951

bufcount : 0.468800067902

opcount : 0.602999973297

因此,对于Windows/Python2.6来说,缓冲区读取策略似乎是最快的。

以下是代码:from __future__ import with_statement

import time

import mmap

import random

from collections import defaultdict

def mapcount(filename):

f = open(filename, "r+")

buf = mmap.mmap(f.fileno(), 0)

lines = 0

readline = buf.readline

while readline():

lines += 1

return lines

def simplecount(filename):

lines = 0

for line in open(filename):

lines += 1

return lines

def bufcount(filename):

f = open(filename)

lines = 0

buf_size = 1024 * 1024

read_f = f.read # loop optimization

buf = read_f(buf_size)

while buf:

lines += buf.count('\n')

buf = read_f(buf_size)

return lines

def opcount(fname):

with open(fname) as f:

for i, l in enumerate(f):

pass

return i + 1

counts = defaultdict(list)

for i in range(5):

for func in [mapcount, simplecount, bufcount, opcount]:

start_time = time.time()

assert func("big_file.txt") == 1209138

counts[func].append(time.time() - start_time)

for key, vals in counts.items():

print key.__name__, ":", sum(vals) / float(len(vals))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值