Performance effect of using print statements in Python script(在 Python 脚本中使用 print 语句的性能效果)
问题描述
我有一个 Python 脚本,它处理一个巨大的文本文件(大约 4 百万行)并将数据写入两个单独的文件.
I have a Python script that process a huge text file (with around 4 millon lines) and writes the data into two separate files.
我添加了一个打印语句,它为每一行输出一个字符串以进行调试.我想知道从性能角度来看它有多糟糕?
I have added a print statement, which outputs a string for every line for debugging. I want to know how bad it could be from the performance perspective?
如果结果很糟糕,我可以删除调试行.
If it is going to very bad, I can remove the debugging line.
编辑
事实证明,对于一个有 400 万行的文件中的每一行都有一个打印语句会增加太多时间.
It turns out that having a print statement for every line in a file with 4 million lines is increasing the time way too much.
推荐答案
为了好玩,试着用一个非常简单的脚本来做,差别是相当惊人的:
Tried doing it in a very simple script just for fun, the difference is quite staggering:
在 large.py 中:
In large.py:
target = open('target.txt', 'w')
for item in xrange(4000000):
target.write(str(item)+'
')
print item
时间安排:
[gp@imdev1 /tmp]$ time python large.py
real 1m51.690s
user 0m10.531s
sys 0m6.129s
gp@imdev1 /tmp]$ ls -lah target.txt
-rw-rw-r--. 1 gp gp 30M Nov 8 16:06 target.txt
现在运行相同的打印"注释掉:
Now running the same with "print" commented out:
gp@imdev1 /tmp]$ time python large.py
real 0m2.584s
user 0m2.536s
sys 0m0.040s
这篇关于在 Python 脚本中使用 print 语句的性能效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 脚本中使用 print 语句的性能效果


基础教程推荐
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01