Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77162 > unrolled thread

Re: iterating over strings seems to be really slow?

Started byTim Chase <python.list@tim.thechases.com>
First post2014-08-27 16:21 -0500
Last post2014-08-27 16:21 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: iterating over strings seems to be really slow? Tim Chase <python.list@tim.thechases.com> - 2014-08-27 16:21 -0500

#77162 — Re: iterating over strings seems to be really slow?

FromTim Chase <python.list@tim.thechases.com>
Date2014-08-27 16:21 -0500
SubjectRe: iterating over strings seems to be really slow?
Message-ID<mailman.13525.1409174624.18130.python-list@python.org>
On 2014-08-27 16:53, Rodrick Brown wrote:
> *I'm confused why the former function runs significantly faster when
> wc1() builds the hash on a single pass and doesn't waste memory of
> returning an array of strings? *
> 
> *I would think wc2() to be slower what's going on here? *
> 
> 
> #!/usr/bin/env python
> 
> s = "The black cat jump over the bigger black cat"
> 
> 
> def wc1():
>     word=""
>     m={}
>     for c in s:
>         if c != " ":
>             word += c

String-building a character-at-a-time is slow.  Also, it doesn't
produce the same results as wc2() does.  Check

  if wc1() == wc2():
    print("Success")
  else:
    print("doh!")

> def wc2():
>     m={}
>     for c in s.split():
>         if m.has_key(c):
>             m[c] += 1
>         else:
>             m[c] = 1
>     return(m)

The thing that surprises me is that using collections.Counter() and
collections.defaultdict(int) are also slower than your wc2():

from collections import defaultdict, Counter

def wc3():
    return Counter(s.split())

def wc4():
    m = defaultdict(int)
    for c in s.split():
        m[c] += 1
    return m

-tkc



[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web