1. 程式人生 > >Python 列表解析 大檔案

Python 列表解析 大檔案

如果在一個4G的大檔案test.log中提取裡面有error的log:

  1. 大家第一想法就是通過開啟檔案然後每一行的查詢,耗時19s
    import  time
    
    start_time = time.time()
    with open('/Users/test/Downloads/test.log') as f:
        for line in f.readlines():
            if 'error' in line:
                print line
    end_time = time.time()
    print "cost time is: ", end_time-start_time
    
    #cost time is:  19.1660439968

     

  2. 列表解析:大多數工作在python直譯器內部完成,比等價的語句要快很多,特別是大檔案,我們可以看見在相同的大檔案中使用列表解析時間為4s,大大提高了效率
    import  time
    
    start_time = time.time()
    lines = [line for line in open('/Users/test/Downloads/test.log') if 'error' in line]
    for line in lines:
        print line
    end_time = time.time()
    print "cost time is: ", end_time-start_time
    
    # cost time is:  4.45141601562