我正在运行带有 gnome 的 Centos 7 桌面,尽管 Arch 和其他发行版经常出现类似的问题。
当我通过 top 检查我的 cpu 使用情况时,pulseaudio 使用了近 20% 的 cpu。
我四处寻找解决方案,但有很多不正确的解决方案,因此我在这里发布业力点以帮助其他人。
你如何以与底层库相当的速度使用 python gzip/gunzip 文件?
tl;dr - Use shutil.copyfileobj(f_in, f_out).
Run Code Online (Sandbox Code Playgroud)
我正在将 *.gz 文件解压缩为更大系列文件处理的一部分,并进行分析以尝试让 python 对内置脚本执行“关闭”。考虑到我正在处理的数据量,这很重要,而且理解起来似乎是一件很重要的事情。
在 ~500MB 上使用 'gunzip' bash 命令产生如下结果:
$time gunzip data.gz -k
real 0m24.805s
Run Code Online (Sandbox Code Playgroud)
一个简单的 python 实现看起来像:
with open('data','wb') as out:
with gzip.open('data.gz','rb') as fin:
s = fin.read()
out.write(s)
real 2m11.468s
Run Code Online (Sandbox Code Playgroud)
不要将整个文件读入内存:
with open('data','wb') as out:
with gzip.open('data.gz','rb') as fin:
out.write(fin.read())
real 1m35.285s
Run Code Online (Sandbox Code Playgroud)
检查本地机器缓冲区大小:
>>> import io
>>> print io.DEFAULT_BUFFER_SIZE
8192
Run Code Online (Sandbox Code Playgroud)
使用缓冲:
with open('data','wb', 8192) as out:
with gzip.open('data.gz','rb', 8192) as fin:
out.write(fin.read())
real 1m19.965s
Run Code Online (Sandbox Code Playgroud)
使用尽可能多的缓冲:
with open('data','wb',1024*1024*1024) …Run Code Online (Sandbox Code Playgroud) centos7 ×1
cpu-usage ×1
espeak ×1
firefox ×1
gunzip ×1
gzip ×1
io ×1
optimization ×1
pulseaudio ×1
python ×1