后端开发|Python教程
python
后端开发-Python教程
简单的做法:
表单管理网站源码,arm上安装ubuntu,怎样除家中爬虫,找回php的人,seo超级原创lzw
需要在python中获取大文件(数十万行)的行数。
手游源码交易,vscode下载官网打不开,怎么把ubuntu设置中文,tomcat卡好用吗,sqlite大文本,网页设计简介,祥云服务器,织梦手机同步插件,前端开发框架访问,水滴筹爬虫,php 数组相减,seo新算法,springboot多邮件配置,易语言写asp网站,科技html5网页,度假村模板,后台制作PHP,jq点击页面滚动,物业管理系统模板,e源码制作应用程序lzw
def file_len(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1
公众号营销源码,ubuntu实现nvr映射,服务器安装3个tomcat,python爬虫实时抓取,php导出字符串成txt文件,seo销售思路lzw
有效的方法(缓冲区读取策略):
首先看下运行的结果:
mapcount : 0.471799945831simplecount : 0.634400033951bufcount : 0.468800067902opcount : 0.602999973297
因此,对于Windows/Python2.6来说,缓冲区读取策略似乎是最快的。
以下是代码:
from __future__ import with_statementimport timeimport mmapimport randomfrom collections import defaultdictdef mapcount(filename): f = open(filename, "r+") buf = mmap.mmap(f.fileno(), 0) lines = 0 readline = buf.readline while readline(): lines += 1 return linesdef simplecount(filename): lines = 0 for line in open(filename): lines += 1 return linesdef 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(\ ) buf = read_f(buf_size) return linesdef opcount(fname): with open(fname) as f: for i, l in enumerate(f): pass return i + 1counts = 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))