gpt4 book ai didi

python - 如果 RAM 不是问题,是逐行读取更快还是将所有内容读入 RAM 并访问它? - Python

转载 作者:太空狗 更新时间:2023-10-29 17:12:42 26 4
gpt4 key购买 nike

如果 RAM 不是问题(我的服务器上有接近 200GB),是逐行读取更快还是将所有内容读入 RAM 并访问它?每行将是大约 200-500 个 unicode 字符的字符串。每个文件有近 200 万行。

逐行

import codecs
for i in codecs.open('unicodefile','r','utf8'):
print i

读入内存

import codecs
for i in codecs.open('unicodefile','r','utf8').readlines():
print i

最佳答案

我在大约 1MB 的字典单词文件上使用了 cProfile。我读了同一个文件 3 次。第一个读取整个文件只是为了将其存储在缓存中的公平竞争环境。这是简单的代码:

def first_read():
codecs.open(file, 'r', 'utf8').readlines()

def line_by_line():
for i in codecs.open(file, 'r', 'utf8'):
pass

def at_once():
for i in codecs.open(file, 'r', 'utf8').readlines():
pass

first_read()
cProfile.run('line_by_line()')
cProfile.run('at_once()')

结果如下:

逐行:

         366959 function calls in 1.762 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.762 1.762 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 codecs.py:322(__init__)
1 0.000 0.000 0.000 0.000 codecs.py:395(__init__)
14093 0.087 0.000 0.131 0.000 codecs.py:424(read)
57448 0.285 0.000 0.566 0.000 codecs.py:503(readline)
57448 0.444 0.000 1.010 0.000 codecs.py:612(next)
1 0.000 0.000 0.000 0.000 codecs.py:651(__init__)
57448 0.381 0.000 1.390 0.000 codecs.py:681(next)
1 0.000 0.000 0.000 0.000 codecs.py:686(__iter__)
1 0.000 0.000 0.000 0.000 codecs.py:841(open)
1 0.372 0.372 1.762 1.762 test.py:9(line_by_line)
13316 0.011 0.000 0.023 0.000 utf_8.py:15(decode)
1 0.000 0.000 0.000 0.000 {_codecs.lookup}
27385 0.027 0.000 0.027 0.000 {_codecs.utf_8_decode}
98895 0.011 0.000 0.011 0.000 {len}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
13316 0.099 0.000 0.122 0.000 {method 'endswith' of 'unicode' objects}
27 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects}
14069 0.027 0.000 0.027 0.000 {method 'read' of 'file' objects}
13504 0.020 0.000 0.020 0.000 {method 'splitlines' of 'unicode' objects}
1 0.000 0.000 0.000 0.000 {open}

一次全部:

         15 function calls in 0.023 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.023 0.023 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 codecs.py:322(__init__)
1 0.000 0.000 0.000 0.000 codecs.py:395(__init__)
1 0.000 0.000 0.003 0.003 codecs.py:424(read)
1 0.000 0.000 0.014 0.014 codecs.py:576(readlines)
1 0.000 0.000 0.000 0.000 codecs.py:651(__init__)
1 0.000 0.000 0.014 0.014 codecs.py:677(readlines)
1 0.000 0.000 0.000 0.000 codecs.py:841(open)
1 0.009 0.009 0.023 0.023 test.py:13(at_once)
1 0.000 0.000 0.000 0.000 {_codecs.lookup}
1 0.003 0.003 0.003 0.003 {_codecs.utf_8_decode}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.001 0.001 0.001 0.001 {method 'read' of 'file' objects}
1 0.010 0.010 0.010 0.010 {method 'splitlines' of 'unicode' objects}
1 0.000 0.000 0.000 0.000 {open}

正如您从结果中看到的那样,一次读取整个文件要快得多,但是您冒着在文件太大时抛出 MemoryError 的风险。

关于python - 如果 RAM 不是问题,是逐行读取更快还是将所有内容读入 RAM 并访问它? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15039380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com