[Python-checkins] CVS: python/dist/src/Lib asynchat.py,1.8,1.9

A.M. Kuchling akuchling@users.sourceforge.net
Wed, 24 Jan 2001 13:10:57 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv11685

Modified Files:
	asynchat.py 
Log Message:
New asynchat.py from Sam Rushing:  this foregoes using the regex module 
   to find the prefix of strings, thus removing a warning, and simply
   uses straightforward string slicing.  


Index: asynchat.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/asynchat.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -r1.8 -r1.9
*** asynchat.py	2001/01/14 18:09:23	1.8
--- asynchat.py	2001/01/24 21:10:55	1.9
***************
*** 1,4 ****
  # -*- Mode: Python; tab-width: 4 -*-
! #       Id: asynchat.py,v 2.25 1999/11/18 11:01:08 rushing Exp
  #       Author: Sam Rushing <rushing@nightmare.com>
  
--- 1,4 ----
  # -*- Mode: Python; tab-width: 4 -*-
! #       Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp 
  #       Author: Sam Rushing <rushing@nightmare.com>
  
***************
*** 280,318 ****
  
  # this could maybe be made faster with a computed regex?
  
- ##def find_prefix_at_end (haystack, needle):
- ##      nl = len(needle)
- ##      result = 0
- ##      for i in range (1,nl):
- ##              if haystack[-(nl-i):] == needle[:(nl-i)]:
- ##                      result = nl-i
- ##                      break
- ##      return result
- 
- # yes, this is about twice as fast, but still seems
- # to be negligible CPU.  The previous version could do about 290
- # searches/sec. the new one about 555/sec.
- 
- import regex
- 
- prefix_cache = {}
- 
- def prefix_regex (needle):
-     if prefix_cache.has_key (needle):
-         return prefix_cache[needle]
-     else:
-         reg = needle[-1]
-         for i in range(1,len(needle)):
-             reg = '%c\(%s\)?' % (needle[-(i+1)], reg)
-         reg = regex.compile (reg+'$')
-         prefix_cache[needle] = reg, len(needle)
-         return reg, len(needle)
- 
  def find_prefix_at_end (haystack, needle):
!     reg, length = prefix_regex (needle)
!     lh = len(haystack)
!     result = reg.search (haystack, max(0,lh-length))
!     if result >= 0:
!         return (lh - result)
!     else:
!         return 0
--- 280,294 ----
  
  # this could maybe be made faster with a computed regex?
+ # [answer: no; circa Python-2.0, Jan 2001]
+ # python:    18307/s
+ # re:        12820/s
+ # regex:     14035/s
  
  def find_prefix_at_end (haystack, needle):
!     nl = len(needle)
!     result = 0
!     for i in range (1,nl):
!         if haystack[-(nl-i):] == needle[:(nl-i)]:
!             result = nl-i
!             break
!     return result