How to speed up MC, Part 2

Wil Dijkstra w.dijkstra at scw.vu.nl
Wed Apr 9 07:28:00 EDT 2003


metacard at lists.runrev.com wrote:
> 
> Very good stuff here, Wil. Now for a question.
> 
> How would this script
> 
>   get line(-1) of k
> 
> compare to
> 
>   get the last line of k
> 
> and to your script below?
> 
> Constructs like "word(-1) of k" and "item -3 to -1 of k" are things I use
> all the time.
> 
> I'd like your analysis, please.
> 
> Thanks!
> 
> Raymond
> 

If x is some negative integer, then:
  get line(x) of k
is equivalent to something like:
  put the number of lines of myList into nLines
  get line nLines + x + 1 of myList
Hence, a statement like
  get line (-1) of myList
will run slower then
  get line (-500) of myList
because in the latter case MC has to count less CR's.

The statement
  get last line of myList
is equivalent to:
  put the number of lines of myList into nLines
  get line nLines of myList
and hence this statement is a fraction faster than
  get line (-1) of myList

All these statements count forward and not backward. That's why the
script G:

  put the number of chars of myList into nChars
  repeat with i = nChars down to 1
    if char i of myList = cr then exit repeat
  end repeat
  get char i + 1 to nChars of myList

is faster: it counts backwards. If you adjust the script to get the same
result as:
  get line -500 of myList
you can be sure that the adjusted script will be much slower in a list
of 1000 lines. But for the one but last line it will still be faster
than 'get line -2 of myList'.

Here are the results of some speed tests on my Mac G4, 500 Mhz, OS 9,
using  a repeat loop of 100000 times, the time being in ticks, the list
consisting of 1000 lines with random numbers between 1 and 1000:

  get last line of myList
850 ticks

  get line -1 of myList
853 ticks

  script G
87 ticks

In performing speed tests you should avoid some common mistakes.
Run your test a number of times. For some reason the speed of the same
test may differ considerably. Repeated testing of the same script may
yield a series of tick counts like: 457,457,456,734,458,735, and so on.
Whether this is due to some background processing of the computer or to
the way MC is compiling the script, I don't know. Anyway, close all
other programs, stop virus scanners, remove ZIP cartridges, disconnect
internet connections, etc.

Further, you should exclude the time the repeat loop itself takes.
Suppose the loop
  repeat 1000000
     test A
  end repeat
takes 50 ticks, whereas

  repeat 1000000
     test B
  end repeat
takes 52 ticks.

However:
  repeat 1000000
  end repeat
may take 14 ticks, hence the actual difference is 36 versus 38 ticks.

Wil Dijkstra



More information about the metacard mailing list