当前位置: 首页 > news >正文

python中readlines()_在Python中,read()或readlines()更快吗?

I want to read a huge file in my code. Is read() or readline() faster for this. How about the loop:

for line in fileHandle

解决方案

For a text file just iterating over it with a for loop is almost always the way to go. Never mind about speed, it is the cleanest.

In some versions of python readline() really does just read a single line while the for loop reads large chunks and splits them up into lines so it may be faster. I think that more recent versions of Python use buffering also for readline() so the performance difference will be minuscule (for is probably still microscopically faster because it avoids a method call). However choosing one over the other for performance reasons is probably premature optimisation.

Edit to add: I just checked back through some Python release notes. Python 2.5 said:

It’s now illegal to mix iterating over

a file with for line in file and

calling the file object’s

read()/readline()/readlines() methods.

Python 2.6 introduced TextIOBase which supports both iterating and readline() simultaneously.

Python 2.7 fixed interleaving read() and readline().

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 递归下降分析程序的设计与实现_算法讲解之递归算法
  • zedgraph 两点之间值_高考必刷题3:计算纬线上两点间的距离
  • 大褂还是内裤
  • wpf datagrid 如何获取当前点击行_「Spire.PDF教程」如何复制 PDF 文档
  • 网络术语还在困扰你吗?
  • python做视觉识别机械手_DIY一个基于树莓派和Python的无人机视觉跟踪系统(转)...
  • 网管之路在何方
  • 判断两个list集合里的对象某个属性值是否一样_BAT面试题——22个集合框架(完)...
  • 在Linux上使用Qt编译WebKit
  • python 进度条_Python 如何实时显示进度条?
  • Massive Customization
  • python 批量下载财务数据_Python+Wind 批量下载上市公司年报 - Part 1/2
  • 使用qmake 单独生成Qt程序
  • 检测同心圆_负氧离子检测仪的工作原理与选择
  • oracle wrost practics
  • [Vue CLI 3] 配置解析之 css.extract
  • 78. Subsets
  • canvas 五子棋游戏
  • export和import的用法总结
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • java2019面试题北京
  • JavaScript设计模式系列一:工厂模式
  • JS基础篇--通过JS生成由字母与数字组合的随机字符串
  • JS数组方法汇总
  • Laravel 实践之路: 数据库迁移与数据填充
  • Less 日常用法
  • PHP的Ev教程三(Periodic watcher)
  • Python学习之路13-记分
  • Sass 快速入门教程
  • supervisor 永不挂掉的进程 安装以及使用
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 关于字符编码你应该知道的事情
  • 排序算法学习笔记
  • 删除表内多余的重复数据
  • 设计模式走一遍---观察者模式
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 微信小程序--------语音识别(前端自己也能玩)
  • 应用生命周期终极 DevOps 工具包
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​软考-高级-系统架构设计师教程(清华第2版)【第9章 软件可靠性基础知识(P320~344)-思维导图】​
  • # 20155222 2016-2017-2 《Java程序设计》第5周学习总结
  • #systemverilog# 之 event region 和 timeslot 仿真调度(十)高层次视角看仿真调度事件的发生
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (¥1011)-(一千零一拾一元整)输出
  • (1)(1.13) SiK无线电高级配置(五)
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (C++17) optional的使用
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (STM32笔记)九、RCC时钟树与时钟 第二部分
  • (zhuan) 一些RL的文献(及笔记)
  • (ZT)一个美国文科博士的YardLife
  • (二)十分简易快速 自己训练样本 opencv级联lbp分类器 车牌识别
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (汇总)os模块以及shutil模块对文件的操作
  • (十一)c52学习之旅-动态数码管