MACSCRPT Archives

April 2008

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"Stockly, Ed" <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Thu, 17 Apr 2008 10:14:55 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (82 lines)
--Does the following change make the script faster or slower? (I don't know
where the command "the ticks" comes from, so am unable to check it here.)

The difference is speed is negligible. I had to repeat each 1000 times to
get a significant and consistent difference. Since, in this case, it runs
only once each time the script runs, that speed difference is irrelevant. I
prefer the modified form of your version below, which is faster than both,
and is more compact than the original.

The deck building routine was cut and pasted from a script that goes back to
OS 7 or maybe later, and in those days the speed difference may have been
greater.

The Ticks is part of Jons Commands, which I've also been using since System
7.

A tick, by the way, is 1/60th of a second. The ticks returns the number of
ticks since the mac was started.

The big boost in speed comes from using "some item of" rather than random
number(x). Random number calls an Osax while some item is native. When I had
more time on my hands I set up macs to run a comparison of the two commands
over night then compared the distribution of numbers and found no visible
bias over millions of iterations.

HTH,

ES
-----------------
set startTime to the ticks
repeat 1000 times
  set fullDeck to {}
  repeat with s in {" of H", " of C", " of S", " of D"}
       repeat with x from 1 to 13
          if x = 1 then
               set c to "A"
            else if x = 11 then
             set c to "J"
            else if x = 12 then
             set c to "Q"
            else if x = 13 then
             set c to "K"
            else
                set c to x as string
            end if
          set the end of fullDeck to c & s
        end repeat
  end repeat
end repeat
set midTime to the ticks
repeat 1000 times
   set fullDeck to {}
  repeat with s in {" of H", " of C", " of S", " of D"}
       repeat with x from 1 to 13
          set c to item x of "A234567890JQK"
          if c = "0" then set c to "10"
           set the end of fullDeck to c & s
        end repeat
  end repeat
end repeat
set endTime to the ticks
return {midTime - startTime, endTime - midTime}
--{184, 248}

-- But this version is slightly faster ({179, 172} with 1000 repeats) than
the original:

repeat with x from 1 to 13
           set c to item x of {"A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K"}
           --if c = "0" then set c to "10"
         set the end of fullDeck to c & s
        end repeat

The speed bump comes from removing the 'if ... then'.

HTH,

ES
--What money I bet in Vegas, stays in Vegas.
=

ATOM RSS1 RSS2