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


Groups > comp.lang.python > #77161

iterating over strings seems to be really slow?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rodrick.brown@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; '&quot;the': 0.07; '__name__': 0.09; 'here?': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; "'__main__':": 0.16; 'subject:slow': 0.16; 'memory': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'pass': 0.26; 'function': 0.29; 'skip:p 30': 0.29; 'array': 0.29; "doesn't": 0.30; 'bigger': 0.30; 'waste': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; '&quot;': 0.31; 'skip:7 10': 0.31; 'skip:# 10': 0.33; '"the': 0.34; 'received:google.com': 0.35; 'returning': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'black': 0.61; '\xc2\xa0\xc2\xa0': 0.74; 'subject:over': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=4UzTMMNiEju3ORtEdX3tOKZ2kc76N3ReBhcEO4p1434=; b=RKNl3COdrn8s0edfn5r4eaSb/udKbAJ2BlV163xtPFG75j7rCLiPA8jWmCStdsMYLO oE+JbHiGSAw422y95vIH239V7UclEe9UE6a/r0C5LujpZq+mqcK3qSReM49p3iBdJMkz JdjG5BjHZsqmyz6jy/4IT/dilcBulEyZJAvgNIZr0yf7T/F/T6Xvz9Tow9rbBGF5Gpt8 GqiCJ3h7RCBtb8MZ7y6v2VO6RlKrU38Z/GLFHgGE5cOjeUUvx0kiej6wBEfHo+RBfhju uhv7R8YS2wD29JgWfuA8DN7Fr5H+z85ZxdhhrwE66Tt0KZxbA9U8Hns/49we40/9ra4o H96g==
X-Received by 10.60.62.234 with SMTP id b10mr37007736oes.3.1409172855102; Wed, 27 Aug 2014 13:54:15 -0700 (PDT)
MIME-Version 1.0
From Rodrick Brown <rodrick.brown@gmail.com>
Date Wed, 27 Aug 2014 16:53:45 -0400
Subject iterating over strings seems to be really slow?
To "python-list@python.org" <python-list@python.org>
Content-Type multipart/alternative; boundary=047d7b45102c74a13b0501a29f40
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.13524.1409172863.18130.python-list@python.org> (permalink)
Lines 317
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409172864 news.xs4all.nl 2905 [2001:888:2000:d::a6]:38979
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77161

Show key headers only | View raw


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

*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
        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)

if __name__ == '__main__':
    import timeit
    print(timeit.timeit("wc1()", setup="from __main__ import wc1"))
    print(timeit.timeit("wc2()", setup="from __main__ import wc2"))

⮀python wordcount.py
7.39647197723
3.15220093727

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


Thread

iterating over strings seems to be really slow? Rodrick Brown <rodrick.brown@gmail.com> - 2014-08-27 16:53 -0400

csiph-web