在Python脚本中使用打印语句的性能影响 [英] Performance effect of using print statements in Python script

查看:152
本文介绍了在Python脚本中使用打印语句的性能影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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:

in large.py:

In large.py:

target =  open('target.txt', 'w')

for item in xrange(4000000):
    target.write(str(item)+'\n')
    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脚本中使用打印语句的性能影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆