本文介绍了在 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 语句的性能效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 20:48