Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Re: Re: Finding dictionary words in a string.

by cchampion (Curate)
on Mar 14, 2004 at 08:02 UTC ( #336471=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Finding dictionary words in a string.
in thread Finding dictionary words in a string.

I can't figure out how you got that speed improvement. The time you save by removing the /i flag is lost when you build a large array in memory (45,000 words in my machine).

Besides, do you really think an all-uppercase output is better?

$ time perl owijfwapplelaskfiwejfcherryalkfwiofwfblossomowiejf
  1 Al
  2 apple
  3 as
  4 ask
  5 blossom
  6 cherry
  7 err
  8 he
  9 her
 10 Herr
 11 Io
 12 Los
 13 loss
 14 mow
 15 of
 16 so
 17 we
0.30user 0.00system 0:00.30elapsed 99%CPU 

$ time perl owijfwapplelaskfiwejfcherryalkfwiofwfblossomowiejf
  1 AL
  3 AS
  4 ASK
  7 ERR
  8 HE
  9 HER
 10 HERR
 11 IO
 12 LOS
 13 LOSS
 14 MOW
 15 OF
 16 SO
 17 WE
0.48user 0.00system 0:00.48elapsed 99%CPU 

Of course, I ran both scripts at least 4 times, to make sure that the storable file was created and that both scripts were reading their input from the disk cache.

  • Comment on Re: Re: Re: Finding dictionary words in a string.

Replies are listed 'Best First'.
Re: Re: Re: Re: Finding dictionary words in a string.
by Anonymous Monk on Mar 15, 2004 at 04:04 UTC
    Not going to chime in with regards to the array in memory, but with regards to the output difference, just store the word in uppercase, as well. Also, don't forget to Code Smarter. Use index, instead of matching with a regex. That should provide the same functionality with nice speed gains.
    #!/usr/bin/perl use strict; use warnings; my $sw = lc shift or die "search word required\n"; my $count = 0; open WORDS, "/usr/share/dict/words" or die "can't open words file\n"; while (<WORDS>) { chomp; printf "%3d %s\n", ++$count, $_ if index($sw,lc $_) > -1; } close WORDS;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://336471]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2017-10-23 19:38 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (284 votes). Check out past polls.