NPR puzzle

Lynch, Jonathan bnz2 at cdc.gov
Wed Jul 20 16:48:18 EDT 2005


I couldn't resist trying this out...

I get a small increase in speed by abandoning the inner loop, allowing
the script to just go to the next word when there is not a match. I am
running a bunch of other programs on my computer right now, so my speed
is compromised. Using Ro's script, I clocked at 75 milliseconds.
Modifying his script as shown below, I improved to 64 milliseconds:

on mouseUp
  put the milliseconds into M
  put fld "W" into W
  put fld "E" into E
  repeat for each line L in W
    if char 1 to 2 of L is not in E then
      next repeat
    else if char 3 to 4 of L is not in E then
      next repeat
    else if char 5 to 6 of L is not in E then
      next repeat
    else if char 7 to 8 of L is not in E then
      next repeat
    else if char 9 to 10 of L is not in E then
      next repeat
    end if
    put L & linefeed after F
  end repeat
  put the milliseconds-M into field "computation time"
  put F into field "output"
end mouseUp


-----Original Message-----
From: use-revolution-bounces at lists.runrev.com
[mailto:use-revolution-bounces at lists.runrev.com] On Behalf Of Alex
Tweedly
Sent: Wednesday, July 20, 2005 4:27 PM
To: How to use Revolution
Subject: Re: NPR puzzle

Lynch, Jonathan wrote:

>Interesting - so 'is not in' was actually faster than lineoffset! Is
>that always the case?
>
>What if you use this:
>
>If matchtext(Gorp,AnElement) = false then...
>  
>
I'd expect that to be slow - matchtext is going to invoke a general 
purpose regex handler - fast for what it does, but slow compared to 
simpler code that just does exact matches on strings.

It's unsurprising that "is not in" would be faster than "lineoffset"m 
because "is in" (or "is not in") can use string searching algorithms, 
which have been extensively studied and optimized, whereas lineoffset 
must either do
  - string search followed by a count of the number of cr's
or
  - loop through the lines, doing a string search within each.

Either way, the simple string search would normally be faster - but I 
wouldn't say "always" faster, there's bound to be some corner case where

it isn't.

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.9.0/50 - Release Date: 16/07/2005

_______________________________________________
use-revolution mailing list
use-revolution at lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your
subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-revolution





More information about the use-livecode mailing list