python统计文件行数

 '''
import time
time_start=time.time()
count=0  
fp=open("base.log","r")  
while 1:  
    buffer=fp.read(8*1024*1024)  
    if not buffer:  
        break  
    count+=buffer.count('\n')  
    #print("count:"+str(count))  
print("all count:"+str(count))
time_end=time.time()
print("use time:", time_end - time_start)
print("over")  
fp.close()
'''
import time
time_start2=time.time()
fr=open("base.log", "r")
line = fr.readline()
count2 = 0
while line:
    #print(line)
    
    line = fr.readline()
    
    count2 = count2 + 1
    #print(str(count))
fr.close()
print("all count2:"+str(count2))
time_end2=time.time()
print("use time:", time_end2 - time_start2)
print("over") 

分为两种法法,前面一种是网上找的比较高大上的方法,发现只有在文件小的时候会效率好

后面一种方法是最朴实的一行一行算,效率还可以。统计的是一个1.5G大小的文件,千万级别,结果如下所示:


Type "copyright", "credits" or "license()" for more information.
>>> 
========= RESTART: E:/02-公司事务资料/2-ORBI/log analysis/count_number.py =========
all count:12303830
use time: 21.817627429962158
over
all count2:12303831
use time: 12.469038963317871
over
>>> 
========= RESTART: E:/02-公司事务资料/2-ORBI/log analysis/count_number.py =========
all count:12303830
use time: 21.56630301475525
over
>>> 
========= RESTART: E:/02-公司事务资料/2-ORBI/log analysis/count_number.py =========
all count2:12303831
use time: 13.03462815284729
over
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值