Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'read.': 0.03; 'cc:addr:python-list': 0.11; 'def': 0.12; 'translation': 0.12; 'correctness': 0.16; 'formatting,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:slow': 0.16; 'wrote:': 0.18; 'first.': 0.19; 'thu,': 0.19; 'version.': 0.19; 'code,': 0.22; '(in': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'easier': 0.31; 'checked': 0.32; 'checking': 0.33; 'style': 0.33; 'something': 0.35; 'definition': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'similar': 0.36; 'behind': 0.37; 'too': 0.37; 'performance': 0.37; 'worry': 0.60; "you're": 0.61; 'first': 0.61; "you've": 0.63; 'direct': 0.67; 'clearer': 0.84; 'subject:over': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=rfdubfy93GwR98Cd2oZrViVhjTBFeHhHnJYF1PIS+zc=; b=mIqYrHcjqj/WwKIl9q2q/IQE5VdJDs13iUo/7od4aLyGrbakwJylNBDxSnidnVEkjB eNad2Gl29mnmiaQ9im9aw7V2ZeCcH9UkusQ1+JGKdlzdnX9pmV6lcCBuvi0M7vhOP6Hf 2mtJHAsmD32bASxLpwAsya4MRSiVAymNIsXlxac6xtG5IpjztIE1en5S8dtvmeBtmve+ 0OmsSTqI+OsA93ySNCW9COQw2DKf5IgSIp7zhyUU3gyxI4SDAiYZqknh5SnznB/pD+N3 8DLGSoP7ClrxNHNlAcfXgaHAQXrdbd8FJBmaxRnk3w9coh/mARX3R4jX60Q6ujrv8ly3 cOeg== MIME-Version: 1.0 X-Received: by 10.43.96.65 with SMTP id cf1mr1067339icc.26.1409180506470; Wed, 27 Aug 2014 16:01:46 -0700 (PDT) In-Reply-To: References: Date: Thu, 28 Aug 2014 09:01:46 +1000 Subject: Re: iterating over strings seems to be really slow? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409180509 news.xs4all.nl 2931 [2001:888:2000:d::a6]:55925 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77175 On Thu, Aug 28, 2014 at 6:53 AM, Rodrick Brown 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) Your code is all buried behind HTML formatting, which makes it hard to read. But frankly, I would first look at writing correct and idiomatic code, unless you're doing a direct translation from C or something (in which case keeping the style as similar as possible to the original makes it easier to verify). The second function is MUCH clearer than the first. Don't worry too much about performance until you've checked that one against some definition of correctness, and then do all your performance checking with a quick correctness check against the idiomatic version. ChrisA