slow Python 3.0 write performance?

Terry Reedy tjreedy at udel.edu
Sat Dec 6 16:13:37 EST 2008


Christian Heimes wrote:
> Istvan Albert wrote:
>> A previous poster suggested that in this case the slowdown is caused
>> by the new io code being written in python rather than C.
> 
> For text mode Python 3's write() method is slower than Python 2.x's 
> method because all text is encoded. The slowdown is mostly caused by 
> additional code for line ending detection and decoding/encoding. The new 
> io library does more than the old file object
> 
> So far the new io library hasn't been optimized yet, too. Please give it 
> some time and report speed issues at http://bugs.python.org/.

On WinXP, tests 3 times each
-------------------------------------------
3.0
---

import time
line = 'a'*99 + '\n'
text = line*500000

start=time.time()
open('wtest', 'w').write(text)
print(time.time()-start)

# 3.4 to 4.0

import time
line = 'a'*99 + '\n'
text = line*500000

start=time.time()
text = bytes(line,'ascii')*500000
print(time.time()-start)

# 1.1 to 1.25

start=time.time()
open('wtest', 'wb').write(text) # note wb
print(time.time()-start)

# 1.5 to 1.8
----------------------------------------------
2.5
---

import time
line = 'a'*99 + '\n'
text = line*500000

start=time.time()
open('wtest', 'w').write(text)
print(time.time()-start)

# 3.3 to 4.1

import time
line = 'a'*99 + '\n'
text = line*500000

start=time.time()
open('wtest', 'wb').write(text) # note wb
print(time.time()-start)

# 1.0 to 1.6
-------------------------------------------------

Conclusion:
1. on WinXP, writing with 3.0 has no slowdown versus 2.5
2. on WinXP, binary mode writing is about 3 times faster that text mode 
writing with its line-ending expansion. If that is not needed, binary 
mode and explicit bytes-text conversion is about twice as fast.

Terry Jan Reedy




More information about the Python-list mailing list