美文网首页
yield vs return

yield vs return

作者: whenitsallover | 来源:发表于2018-04-02 00:14 被阅读0次
    import memory_profiler
    import time
    
    
    start = time.time()
    print("Before {}Mb".format(memory_profiler.memory_usage()))
    
    def calculation(para):
        result = []
        for i in para:
            result.append(i)
        return result
    
    
    ''' 
    return  [52.0] Mb  [56.25390625]Mb           1.644s
    yield [51.86328125]Mb  [51.87109375]Mb  0.202s 
    '''
    res = calculation(range(10000000))
    print("After {}Mb".format(memory_profiler.memory_usage()))
    print(time.time()-start)
    
    

    Obviously, generator can boost the performance by saving your memory. It's not holding all of the results in memory. But once you use the list() function, you'll lose this advantage.

    相关文章

      网友评论

          本文标题:yield vs return

          本文链接:https://www.haomeiwen.com/subject/fjgzcftx.html