yEnc implementation in Python, bit slow

Freddie oinkfreddie at oinkshlick.oinknet
Mon Aug 4 10:50:58 EDT 2003


Hi,

I posted a while ago for some help with my word finder program, which is now 
quite a lot faster than I could manage. Thanks to all who helped :)

This time, I've written a basic batch binary usenet poster in Python, but 
encoding the data into yEnc format is fairly slow. Is it possible to improve 
the routine any, WITHOUT using non-standard libraries? I don't want to have 
to rely on something strange ;)

yEncode1 tends to be slightly faster here for me on my K6/2 500:

$ python2.3 testyenc.py
yEncode1 401563 1.82
yEncode1 401563 1.83
yEncode2 401562 1.83
yEncode2 401562 1.83

Any help would be greatly appreciated :)

Freddie


import struct
import time
from zlib import crc32

def timing(f, n, a):
    print f.__name__,
    r = range(n)
    t1 = time.clock()
    for i in r:
		#f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a)
		f(a)
    t2 = time.clock()
    print round(t2-t1, 3)

def yEncSetup():
	global YENC
	YENC = [''] * 256
	
	for I in range(256):
		O = (I + 42) % 256
		if O in (0, 10, 13, 61):
			# Supposed to modulo 256, but err, why bother?
			O += 64
			YENC[I] = '=%c' % O
		else:
			YENC[I] = '%c' % O

def yEncode1(data):
	global YENC
	yenc = YENC
	
	encoded = []
	datalen = len(data)
	n = 0
	while n < datalen:
		chunk = data[n:n+256]
		n += len(chunk)
		encoded.extend([yenc[ord(c)] for c in chunk])
		encoded.append('\n')
	
	print len(''.join(encoded)),

def yEncode2(data):
	global YENC
	yenc = YENC
	
	lines = []
	datalen = len(data)
	n = 0
	
    	bits = divmod(datalen, 256)
	format = '256s' * bits[0]
	parts = struct.unpack(format, data[:-bits[1]])
	for part in parts:
		lines.append(''.join([yenc[ord(c)] for c in part]))
	
	lines.append(''.join([yenc[ord(c)] for c in data[-bits[1]:]]))
	print len('\n'.join(lines) + '\n'),


yEncSetup()

teststr1 = 'a' * 400000
teststr2 = 'b' * 400000

for meth in (yEncode1, yEncode2):
	timing(meth, 1, teststr1)
	timing(meth, 1, teststr2)

-- 
Remove the oinks!




More information about the Python-list mailing list