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


Groups > comp.lang.python > #77163

Re: iterating over strings seems to be really slow?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ckaynor@zindagigames.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; '&quot;the': 0.07; 'duplicate': 0.07; 'string': 0.09; '&quot;)': 0.09; 'skip:2 30': 0.09; 'slow.': 0.09; 'trailing': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"in"': 0.16; 'collections': 0.16; 'repetitions': 0.16; 'space)': 0.16; 'subject:slow': 0.16; 'surprises': 0.16; 'to:addr:python.list': 0.16; 'to:addr:tim.thechases.com': 0.16; 'to:name:tim chase': 0.16; 'variations': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'aug': 0.22; 'cc:addr:gmail.com': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**1': 0.23; '&gt;&gt;&gt;': 0.24; 'switched': 0.24; 'versions': 0.24; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'tim': 0.29; 'words': 0.29; 'bigger': 0.30; 'message-id:@mail.gmail.com': 0.30; '&quot;': 0.31; 'chase': 0.31; 'keyerror:': 0.31; 'provided,': 0.31; 'reduced': 0.31; 'option': 0.32; 'running': 0.33; '"the': 0.34; 'received:74.125.82': 0.34; 'except': 0.35; 'received:google.com': 0.35; 'version': 0.36; '8bit%:9': 0.36; 'found.': 0.36; 'words,': 0.36; 'subject:?': 0.36; 'skip:4 10': 0.37; 'two': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expect': 0.39; 'received:74.125': 0.39; 'skip:b 40': 0.39; 'skip:n 30': 0.60; 'black': 0.61; 'skip:t 30': 0.61; 'skip:* 10': 0.61; 'times': 0.62; 'skip:6 10': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'skip:1 20': 0.65; 'here': 0.66; 'presented': 0.69; 'increase': 0.74; 'skip:n 40': 0.81; 'subject:over': 0.84
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=/vymTEUVSCTB7c/4F+MHUMalPFcF/VJYHhHFl6iOXSc=; b=SK1PpIf5YF3kYLhhC1ad9ry1DBbN3e4SCl+4VzwGeoLv6ODGZhS3d22kwHW+41BAXg UMGY/LrQq3FTBYUMQ8/M3WgsG5DumlWa+9tUrZZRydELuGJ1l/uAMyVqOtsbQPjH7Ril 0P3wMg0bZITk5vf8SdIlJtyeOLuzRg089iJDNTk6ujijy/HRx1ApsHTVd4hslWZddURc VR+sBCoi575jWSwrlv1CVQgX/LRYH5+t/lWjlM47X6ASD+K+0l5Qd2dsDiGSA+8fARGr 6R5/8hltCDfXSdNUrHzUPR6c2q3rfpWI69Lm/86NSXCyJwgRU+/LxYSICVz0KXiAAcmz +r3w==
X-Gm-Message-State ALoCoQlYLe15u1mXdKxHrLYVzRMKEGjAvs56Fp7P+h+KcPGzYlIic2qA5WLpGeeFhn69K+gFHzXZ
X-Received by 10.180.91.70 with SMTP id cc6mr916298wib.66.1409176279465; Wed, 27 Aug 2014 14:51:19 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <20140827162157.72e20091@bigbox.christie.dr>
References <CABRP1o-X3wadeNh-7sf7XvEFGAus-QLQ=FTEab3hRT+y2EzyWA@mail.gmail.com> <20140827162157.72e20091@bigbox.christie.dr>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Wed, 27 Aug 2014 14:50:59 -0700
Subject Re: iterating over strings seems to be really slow?
To Tim Chase <python.list@tim.thechases.com>
Content-Type multipart/alternative; boundary=f46d0438edd79057be0501a36b35
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13526.1409176281.18130.python-list@python.org> (permalink)
Lines 262
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409176281 news.xs4all.nl 2886 [2001:888:2000:d::a6]:46171
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77163

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Wed, Aug 27, 2014 at 1:53 PM, Rodrick Brown <rodrick.brown@gmail.com>
 wrote:

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


On Wed, Aug 27, 2014 at 2:21 PM, Tim Chase <python.list@tim.thechases.com>
wrote:
>
> 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
>

I ran a couple more experiments, and, at least on my machine, it has to do
with the number of duplicate words found. I also added two more variations
of my own:

def wc5(s): # I expect this one to be slow.
m = {}
for c in s.split():
 m.setdefault(c, 0)
m[c] += 1
return m

def wc6(s): # This one might be better than any other option presented so
far.
m = {}
 for c in s.split():
try:
m[c] += 1
 except KeyError:
m[c] = 1
return m

I also switched the OP's versions to use the "in" operator rather than
has_key, as I am running Python 3.4.1.

With the same dataset (plus a trailing space) the OP provided, here are my
times: (s = "The black cat jump over the bigger black cat ")

>>> timeit.timeit("wc1(s)", setup=setup, number=1000000)
6.076951338314008
*>>> timeit.timeit("wc2(s)", setup=setup, number=1000000)*
*2.451220378346954*
>>> timeit.timeit("wc3(s)", setup=setup, number=1000000)
5.249674617410577
>>> timeit.timeit("wc4(s)", setup=setup, number=1000000)
3.531042215121076
>>> timeit.timeit("wc5(s)", setup=setup, number=1000000)
3.4734603842861205
>>> timeit.timeit("wc6(s)", setup=setup, number=1000000)
4.322543365103378


When I increase the data set by multipling the OP's string 1000 times (s =
"The black cat jump over the bigger black cat "*1000), here are the times
(I reduced the number of repetitions to keep the time reasonable):

>>> timeit.timeit("wc1(s)", setup=setup, number=1000)
5.807871555058417
>>> timeit.timeit("wc2(s)", setup=setup, number=1000)
2.3245083748933535
*>>> timeit.timeit("wc3(s)", setup=setup, number=1000)*
*1.5722138905703211*
>>> timeit.timeit("wc4(s)", setup=setup, number=1000)
1.901478857657942
>>> timeit.timeit("wc5(s)", setup=setup, number=1000)
3.065888476414475
>>> timeit.timeit("wc6(s)", setup=setup, number=1000)
2.0125233934956217


It seems that with a large number of duplicate words, the counter version
is the best, however with fewer duplicates, the contains check is better.

Chris

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: iterating over strings seems to be really slow? Chris Kaynor <ckaynor@zindagigames.com> - 2014-08-27 14:50 -0700

csiph-web