Frankenstring

ucntcme at gmail.com ucntcme at gmail.com
Thu Jul 14 17:55:45 EDT 2005


Here is a cStringIO based version:
class FrankenString:
	def __init__(self,string=None):
		self.str = StringIO(string)
		self.atEnd = False
		self.lastidx = 0
		self.seek = self.str.seek
		self.tell = self.str.tell
	def __iter__(self):
		while not self.atEnd:
			char =  self.str.read(1)
			idx = self.str.tell()
			if self.lastidx == idx:
				self.atEnd = True
			self.lastidx = idx
			yield char

On a string 1024*1024 long the StringIO version takes 10s to iterate
over  but do nothing, whereas this one takes 3.1 on my system. Well to
create that string, create the instance and loop over. But since each
variant is doing the same thing I figure it's even. ;)




More information about the Python-list mailing list