2015-09-07 25 views
2

我運行下面的程序慢17%:多線程cstringio是在Ubuntu 14.04

import cStringIO 
import time 
import threading 

def func(tid): 
    buff = 'a'*4096 
    i = 0 
    while (i < 40000000): 
     output = cStringIO.StringIO() 
     output.write(buff) 
     contents = output.getvalue() 
     output.close() 
     i = i + 1 

threads = 16 
threadlist = [] 

start = time.time() 
for tc in range(threads): 
    thr = threading.Thread(target=func, args=(tc,)) 
    threadlist.append(thr) 
    thr.start() 

for thr in threadlist: 
    thr.join() 

end = time.time() 
print "Time taken is %s" % (end - start) 

的機器上與然而完全相同的硬件一個運行Ubuntu 10.04和其他運行14.04。我觀察到它在10.04上需要1409.54秒,而在14.04上需要1656.81秒,在14.04上表現出17%的性能下降。有任何想法嗎?

+0

您是否嘗試轉換爲python3?與此同時,python3已經有了主要的性能改進。另一點是,你是否檢查每個系統上的Python版本? –

+0

@BeowulfOF python3不是主要的選擇,因爲它目前提供有限的軟件包支持。我已經在兩臺機器上安裝並使用了相同的Python版本,即2.7.8。 –

+0

我會建議使用['timeit'](https://docs.python.org/2/library/timeit.html)模塊再次分析程序。就像比較一樣。 –

回答

0

此行爲是由於14.04上的超線程。有趣的是,在我的2核心機器上禁用超線程(並且有效地在單個超線程上運行)之後,14.04的性能與10.04相當。