资讯专栏INFORMATION COLUMN

pythonmemory_profiler库制作器和迭代器cpu占用的时间分析

89542767 / 234人阅读

  文章内容主要是详细介绍了pythonmemory_profiler库制作器和迭代器cpu占用的时间分析,文章内容紧扣主题进行详尽的基本介绍,感兴趣的朋友可以了解一下


  不进行计算时,生成器和list空间占用


  import time
  from memory_profiler import profile
  profile(precision=4)
  def list_fun():
  start=time.time()
  total=([i for i in range(5000000)])
  print('iter_spend_time:',time.time()-start)
  profile(precision=4)
  def gent_func():
  gent_start=time.time()
  total=(i for i in range(5000000))
  print('gent_spend_time:',time.time()-gent_start)
  iter_fun()
  gent_func()

01.png

  显示结果的内涵:第一行表明已剖析编码的号码,第二列(Mem应用情况)表明实行当列后Python编译器的内存使用情况。第三列(增长)表明现阶段行相较于最后一行的运行内存差别。最终某列(行具体内容)打印出已讲解的编码。


  剖析:在没有来计算的情形下,目录list和迭代器会占空间,但是对于制作器不容易占空间


  当要测算,list和制作器的耗费时间和占用内存


  使用sum内置函数,list和制作器求合10000000个数据信息,listcpu占用比较大,制作器耗费时间大约是list的二倍


  import time
  from memory_profiler import profile
  profile(precision=4)
  def iter_fun():
  start=time.time()
  total=sum([i for i in range(10000000)])
  print('iter_spend_time:',time.time()-start)
  profile(precision=4)
  def gent_func():
  gent_start=time.time()
  total=sum(i for i in range(10000000))
  print('gent_spend_time:',time.time()-gent_start)
  iter_fun()
  gent_func()

02.png

  对比分析,必要时对信息进行迭代更新使用中,制作器方式的用时很长,但内存使用上还是偏少,由于应用制作器时,运行内存只存放每一次迭代计算的信息。查找原因时个人觉得,制作器的迭代计算环节中,在迭代更新数据与测算立即持续变换,对比与迭代器目标中先将它们所有储存在运行内存中(尽管占用内存,但载入比再度迭代更新要快点),因而,制作器较为耗时间,但占用内存小。


  记录数据循环系统求合500000个数据信息,迭代器和制作器循环系统得到时


  汇总:基本上与此同时成功,迭代器的占用内存比较大


  import time
  from memory_profiler import profile
  itery=iter([i for i in range(5000000)])
  gent=(i for i in range(5000000))
  profile(precision=4)
  def iter_fun():
  start=time.time()
  total=0
  for item in itery:
  total+=item
  print('iter:',time.time()-start)
  profile(precision=4)
  def gent_func():
  gent_start=time.time()
  total=0
  for item in gent:
  total+=item
  print('gent:',time.time()-gent_start)
  iter_fun()
  gent_func()
  list,迭代器和生成器共同使用sum计算5000000个数据时间比较

03.png

  总结:list+sum和迭代器+sum计算时长差不多,但生成器+sum计算的时长几乎长一倍,


  import time
  from memory_profiler import profile
  profile(precision=4)
  def list_fun():
  start=time.time()
  print('start!!!')
  list_data=[i for i in range(5000000)]
  total=sum(list_data)
  print('iter_spend_time:',time.time()-start)
  profile(precision=4)
  def iter_fun():
  start=time.time()
  total=0
  total=sum(iter([i for i in range(5000000)]))
  print('total:',total)
  print('iter_spend_time:',time.time()-start)
  profile(precision=4)
  def gent_func():
  gent_start=time.time()
  total=sum(i for i in range(5000000))
  print('total:',total)
  print('gent_spend_time:',time.time()-gent_start)
  list_fun()
  iter_fun()
  gent_func()

04.png

  综上所述,这篇文章就给大家介绍到这里了,希望可以给大家带来帮助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128837.html

相关文章

  • PHP迭代生成

    摘要:生成器的内部一直在停顿和恢复之间切换,直到循环完成或停顿位置缺点生成器不能满足所有迭代器的需求,因为如果不查询,生成器永远不知道下一个要迭代的值是什么,在生成器中无法后退或前进。 一.迭代器 分析:想一下,如果把集合对象和对集合对象的操作放在一起,当我们想换一种方式遍历集合对象中元素时,就需要修改集合对象了,违背单一职责原则,而迭代器模式将数据结构和数据结构的算法分离开,两者可独立发展...

    sanyang 评论0 收藏0
  • python奇遇记:迭代生成

    摘要:来说说迭代器和生成器,还有可迭代对象和生成器表达式。有点绕是不是,其实,一般只要知道可迭代对象以及它是如何实现的就行了,中常常用生成器来代替迭代器,可以说,生成器就是迭代器。 来说说迭代器和生成器,还有可迭代对象和生成器表达式。 之前简单的提到过,一个对象是可迭代的可以理解为能够使用for循环。这样说其实不太准确,某个对象可迭代是因为它内部实现了$__iter__$这个特殊方法。比如在...

    atinosun 评论0 收藏0
  • Python中迭代对象、迭代生成

    摘要:本文重点掌握可迭代的对象的定义掌握可迭代对象迭代器与生成器之间的关系和异同熟悉标准库中生成器。二迭代器迭代器介绍迭代器用于从集合中取出元素的对象。若想再次迭代须重建迭代器。迭代器检查方式调用,。区别可迭代的对象不是迭代器。 导语:本文章记录了本人在学习Python基础之控制流程篇的重点知识及个人心得,打算入门Python的朋友们可以来一起学习并交流。 本文重点: 1、掌握可迭代的对象的...

    starsfun 评论0 收藏0
  • 当我们调用yield,它究竟做了什么

    摘要:显然,要理解,首先要了解迭代器,接着了解什么是生成器。生成器上述代码中,就是一个迭代器,循环部分就是迭代过程。迭代器和生成器的执行效率因为生成器边迭代边生成,所以占用内存极少,执行效率也更高。 显然,要理解yield,首先要了解迭代器(iterator),接着了解什么是生成器(generator)。 迭代器 通俗的讲,迭代器就是可以逐个访问的容器,而逐个逐步访问的过程成为迭代。 ite...

    Jochen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<