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


Groups > comp.lang.python > #77175

Re: iterating over strings seems to be really slow?

References <CABRP1o-X3wadeNh-7sf7XvEFGAus-QLQ=FTEab3hRT+y2EzyWA@mail.gmail.com>
Date 2014-08-28 09:01 +1000
Subject Re: iterating over strings seems to be really slow?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.13535.1409180509.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Aug 28, 2014 at 6:53 AM, 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)

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

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


Thread

Re: iterating over strings seems to be really slow? Chris Angelico <rosuav@gmail.com> - 2014-08-28 09:01 +1000

csiph-web