Python中,將字元串寫到文件可以通過一下兩種方式實現: 1、print >> log_file, "Hello world! " 2、log_file.write("Hello world! ") 這兩種方式在性能上有差別么?那個更快?
經過測試是write速度更快。具體測試過程如下:
# -*- coding: utf-8 -*-
from time import clock
def speedTest(func):
def _deco():
startTime = clock()
func()
endTime = clock()
print "Cost time is " + str(endTime - startTime)
return _deco
@speedTest
def method1():
with open(a.txt,w) as fp:
for x in xrange(1000000):
fp.write("Hello world!
")
@speedTest
def method2():
with open(b.txt,w) as fp:
for x in xrange(1000000):
print &>&> fp, "Hello world!"
print "write method:"
method1()
print
print ">&> method:"
method2()
但是需要指出題主的一個問題,就是print是會自己在行末加上一個換行符的。所以測試的時候需要考慮這個誤差。
至於為什麼write更快,在python的官方文檔是這麼描述的:
print also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as 「print chevron.」 In this form, the first expression after the &>&> must evaluate to a 「file-like」 object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.
補充:個人猜想。
print &>&> expression1, expression2 比直接write做多了一樣事情,evaluate the first expression after &>&> is file like object, then write.6. Simple statementsprint also has an extended form, defined by the second portion of the syntax described above. This form is sometimes referred to as 「print chevron.」 In this form, the first expression after the &>&> must evaluate to a 「file-like」 object, specifically an object that has a write() method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates to None, then sys.stdout is used as the file for output.
為什麼不自己benchmark一下
write快
測試後的結果。可能的原因,具體不是很清楚:print &>&> log_file,"Hello world!
"是一個重定向形式,是將"Hello world!
"的值輸出到log_file的write方法中,可能這個過程比直接write要多消耗時間吧。
推薦閱讀:
※花式玩轉多維矩陣|入門這篇就夠了
※如何評價 Quora 的 Ultralisk 並行架構?
※利用python進行數據分析之數據載入、存儲與文件格式(五)
※如何優雅的閱讀openstack源代碼?