我正在编写一个简单的应用程序,它将大文本文件拆分为较小的文件,并且我已经编写了2个版本,一个使用列表,另一个使用生成器。我使用memory_profiler模块对这两个版本进行了概要分析,并清楚地显示了生成器版本的更好的内存效率,但是在对使用生成器的版本进行概要分析时,这很奇怪,这增加了执行时间。下面的演示解释了我的意思

使用列表版本

frommemory_profilerimportprofile@profile()defmain():file_name=input("Enter the full path of file you want to split into smaller inputFiles: ")input_file=open(file_name).readlines()num_lines_orig=len(input_file)parts=int(input("Enter the number of parts you want to split in: "))output_files=[(file_name+str(i))foriinrange(1,parts+1)]st=0p=int(num_lines_orig/parts)ed=pforiinrange(parts-1):withopen(output_files[i],"w")asOF:OF.writelines(input_file[st:ed])st=ed

ed=st+pwithopen(output_files[-1],"w")asOF:OF.writelines(input_file[st:])if__name__=="__main__":main()

与分析器一起运行时

$ time py36Splitting\ text\ files_BAD_usingLists.pyEnterthe full path of file you want to split into smaller inputFiles:/apps/nttech/rbhanot/Downloads/test.txtEnterthe number of parts you want to splitin:3Filename:Splittingtext files_BAD_usingLists.pyLine# Mem usage Increment Line Contents================================================647.8MiB0.0MiB@profile()7defmain():847.8MiB0.0MiBfile_name=input("Enter the full path of file you want to split into smaller inputFiles: ")9107.3MiB59.5MiBinput_file=open(file_name).readlines()10107.3MiB0.0MiBnum_lines_orig=len(input_file)11107.3MiB0.0MiBparts=int(input("Enter the number of parts you want to split in: "))12107.3MiB0.0MiBoutput_files=[(file_name+str(i))foriinrange(1,parts+1)]13107.3MiB0.0MiBst=014107.3MiB0.0MiBp=int(num_lines_orig/parts)15107.3MiB0.0MiBed=p16108.1MiB0.7MiBforiinrange(parts-1):17107.6MiB-0.5MiBwithopen(output_files[i],"w")asOF:18108.1MiB0.5MiBOF.writelines(input_file[st:ed])19108.1MiB0.0MiBst=ed20108.1MiB0.0MiBed=st+p2122108.1MiB0.0MiBwithopen(output_files[-1],"w")asOF:23108.1MiB0.0MiBOF.writelines(input_file[st:])real0m6.115suser0m0.764ssys0m0.052s

在没有分析器的情况下运行

$ time py36Splitting\ text\ files_BAD_usingLists.pyEnterthe full path of file you want to split into smaller inputFiles:/apps/nttech/rbhanot/Downloads/test.txtEnterthe number of parts you want to splitin:3real0m5.916suser0m0.696ssys0m0.080s

现在使用发电机

@profile()defmain():file_name=input("Enter the full path of file you want to split into smaller inputFiles: ")input_file=open(file_name)num_lines_orig=sum(1for_ininput_file)input_file.seek(0)parts=int(input("Enter the number of parts you want to split in: "))output_files=((file_name+str(i))foriinrange(1,parts+1))st=0p=int(num_lines_orig/parts)ed=pforiinrange(parts-1):file=next(output_files)withopen(file,"w")asOF:for_inrange(st,ed):OF.writelines(input_file.readline())st=ed

ed=st+pifnum_lines_orig-ed

file=next(output_files)withopen(file,"w")asOF:for_inrange(st,ed):OF.writelines(input_file.readline())if__name__=="__main__":main()

使用分析器选项运行时

$ time py36-m memory_profilerSplitting\ text\ files_GOOD_usingGenerators.pyEnterthe full path of file you want to split into smaller inputFiles:/apps/nttech/rbhanot/Downloads/test.txtEnterthe number of parts you want to splitin:3Filename:Splittingtext files_GOOD_usingGenerators.pyLine# Mem usage Increment Line Contents================================================447.988MiB0.000MiB@profile()5defmain():647.988MiB0.000MiBfile_name=input("Enter the full path of file you want to split into smaller inputFiles: ")747.988MiB0.000MiBinput_file=open(file_name)847.988MiB0.000MiBnum_lines_orig=sum(1for_ininput_file)947.988MiB0.000MiBinput_file.seek(0)1047.988MiB0.000MiBparts=int(input("Enter the number of parts you want to split in: "))1148.703MiB0.715MiBoutput_files=((file_name+str(i))foriinrange(1,parts+1))1247.988MiB-0.715MiBst=01347.988MiB0.000MiBp=int(num_lines_orig/parts)1447.988MiB0.000MiBed=p1548.703MiB0.715MiBforiinrange(parts-1):1648.703MiB0.000MiBfile=next(output_files)1748.703MiB0.000MiBwithopen(file,"w")asOF:1848.703MiB0.000MiBfor_inrange(st,ed):1948.703MiB0.000MiBOF.writelines(input_file.readline())202148.703MiB0.000MiBst=ed2248.703MiB0.000MiBed=st+p2348.703MiB0.000MiBifnum_lines_orig-ed

python代码性能分析_使用memory_profiler对代码进行性能分析会增加执行时间相关推荐

  1. Windows五种IO模型性能分析和Linux五种IO模型性能分析

    Windows五种IO模型性能分析和Linux五种IO模型性能分析 http://blog.csdn.net/jay900323/article/details/18141217 http://blo ...

  2. code blocks代码性能分析_记一次Python Web接口优化,性能提升25倍!

    背景 我们负责的一个业务平台,有次在发现设置页面的加载特别特别地慢,简直就是令人发指 让用户等待 36s 肯定是不可能的,于是我们就要开启优化之旅了. 投石问路 既然是网站的响应问题,可以通过 Chr ...

  3. 用python进行营销分析_用python进行covid 19分析

    用python进行营销分析 Python is a highly powerful general purpose programming language which can be easily l ...

  4. python生日悖论分析_生日悖论

    python生日悖论分析 If you have a group of people in a room, how many do you need to for it to be more like ...

  5. python基金预测分析_基金定投选星期几更划算?[python统计分析]

    基金定投常见的一种方式是定期定额投资,即每周或每月固定的时间段,向基金公司申购固定份额的基金.基金定投可以平均成本.分散风险,实现自动投资,所以基金定投又称为"懒人投资术".今天主 ...

  6. 【ceph】ceph性能分析工具之perf dump代码打点调试统计

    目录 简介 一.查ceph自带的统计 1 命令格式 2 dump 命令输出结果 二.自己添加统计 三.分析案例 io耗时初分析 继续分析 进一步分析 代码修改和验证 四.报错记录 五.附录 1.代码修 ...

  7. jmeter性能分析_使用JMeter和Yourkit进行REST / HTTP服务的性能分析

    jmeter性能分析 我的上一篇文章描述了如何使用JMeter完成异步REST / HTTP服务的压力测试或负载测试. 但是,运行这样的测试通常表明被测系统不能很好地应对增加的负载. 现在的问题是如何 ...

  8. mysql 性能分析_十大MySQL性能分析工具汇总!专治MySQL性能瓶颈

    前言 MySQL 数据库最常见的两个瓶颈是CPU和I/O的瓶颈.CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候,磁盘I/O瓶颈发生在装入数据远大于内存容量的时候. MySQL数据库性能 ...

  9. python猫眼电影分析_抓取猫眼电影

    注意:抓取内容之前一定要查看下Robots协议 1.准备工作 1>第一步,安装python,安装requests.json库. 2.抓取分析 1>接下来我们打开网页分析下猫眼电影排行榜 2 ...

最新文章

  1. Hadoop - MapReduce MRAppMaster-剖析
  2. Azure上用API成功创建Lambda Function的截图
  3. 【HDU - 5889】Barricade(最短路+网络流,最小割)
  4. Tuxedo中间件介绍
  5. thinkpad解决Win8.1电源管理的方法
  6. Sass!默认和主题化的设计系统
  7. K.gather()
  8. 刚装新系统环境mscorsvw.exe进程占用CPU资料50%以上的原因
  9. 0~9生成随机数4位数
  10. Windows Server 2016搭建文件服务器
  11. 轻松一下,做到题目吧。
  12. 部署hexo遇到报错ERROR Deployer not found: git的解决办法
  13. 1418 -This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration
  14. 【tools】Beyondcompared 3 试用过期 修改注册表
  15. C语言——二维数组每行求和
  16. Top k问题(线性时间选择算法)
  17. ROS中gmapping建图教程
  18. windows驱动inf文件详解
  19. javabean和xml相互转化的工具类
  20. 如何查看智能手机的IP地址

热门文章

  1. java 文件流读取文本_如何在Java 8中处理流和读取文本文件
  2. spring jaxb_自定义Spring命名空间使JAXB变得更容易
  3. java低延迟_像Java这样的C ++具有低延迟
  4. java内存模型和内存结构_Java内存模型和优化
  5. 什么是JAX-RS注释? (第2部分)
  6. days to_days_Java2Days 2012:Java EE
  7. stringutils_番石榴分配器vs StringUtils
  8. Spring MVC 4快速入门Maven原型得到了改进–更多Java 8功能
  9. Java持久性锁定初学者指南
  10. 使对易失性字段的操作原子化