There's got to be an easy way to do this (fwd)

Lulu of the Lotus-Eaters mertz at gnosis.cx
Thu Jul 5 17:42:00 EDT 2001


Still dying to win the performance prize, I believe Skip Montanaro's
performance tip brings the general filter stategy back up to winning
status (including James Logajan 'traditional' function)

  def str_join2(iters):
      has = {0:1,1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1}.has_key
      for i in iters:
          "".join([x for x in '(123)/456-7890' if has(x)])

  def flt_lmbda3(iters):
      isdigit = {0:1,1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1}.has_key
          for i in iters:
              filter(isdigit, '(123)/456-7890')

  def traditional(iters):
      for i in iters:
          result = ""
          for c in '(123)/456-7890':
              if c in digits:
                  result = result + c

RESULTS:

-   500-       str_join    str_join2   flt_lmbda2  flt_lmbda3  traditional
         5 :    0.10         0.07         0.10         0.06         0.08
        10 :    0.21         0.15         0.21         0.10         0.16
        50 :    1.04         0.73         1.02         0.57         0.79





More information about the Python-list mailing list