Count, number, length [was Re: Settings backup script]
Count, number, length [was Re: Settings backup script]
- Subject: Count, number, length [was Re: Settings backup script]
- From: Nigel Garvey <email@hidden>
- Date: Fri, 3 Aug 2001 17:23:04 +0100
Shane Stanley wrote on Fri, 03 Aug 2001 13:52:41 +1000:
>
On 3/8/01 12:05 PM +1000, Nigel Garvey, email@hidden,
>
wrote:
>
>
> It's the characters of a string that are counted by default, so 'number of
>
> MyComments', or 'length of MyComments', or 'count MyComments' will tell
>
you
>
> how many characters there are in the string. The expression 'number of
>
> characters of MyComments' is the same as 'number of (characters of
>
> MyComments)'. It first makes a list of the individual characters - eg.
>
{"H",
>
> "e", "l", "l", "o"} - and then counts the items in the list. The answer's
>
> exactly the same, but it uses more memory and takes a little longer.
>
>
I'm not entirely convinced of this, although I'm open to persuasion. But if
>
I run the following script:
>
>
set x to "The wiley brown fox scripted his way around the lazy black dog."
>
set time1 to the ticks
>
repeat 10000 times
>
set a to characters of x
>
end repeat
>
set time2 to the ticks
>
repeat 10000 times
>
set a to count of characters of x
>
end repeat
>
set time3 to the ticks
>
repeat 10000 times
>
set a to length of x
>
end repeat
>
set time4 to the ticks
>
repeat 10000 times
>
set a to count of x
>
end repeat
>
set time5 to the ticks
>
{time2 - time1, time3 - time2, time4 - time3, time5 - time4}
>
>
I typically get something like {117, 13, 12, 10}.
Yes, that's very dramatic! I'm typically getting {838, 64, 48, 44}, which
shows that I'm probably wrong about the list in the case of 'count' - at
least as regards doing it 10000 times in succession. However, changing
the second test to 'number of characters of x' and the fourth to 'number
of x', the terms used in my post, I get {835, 876, 49, 68}, which is more
nearly consistent with my theory.
>
This suggests to me that:
>
>
(a) "count of characters of" doesn't actually build a list, given how long
>
just building a list takes;
Conceded. The alternative form 'count x each character' is faster still.
However, a straight 'count x' is still best if the character count is
what you want. It seems not to check that each character is a character
or that each item is an item.
>
(b) getting "length of" is not appreciably faster than "count of characters
>
of"; and
It takes about 3/4 to 5/6 the time on this machine. In a straight race
without 'characters of', 'count x' is faster than 'length of x' is faster
than 'number of x'.
>
(c) although "count of" is the fastest method, there's not much in it.
Agreed - though I've found in the past that if x is a reference to a very
long list, 'length of' sometimes has a considerable advantage.
>
I guess this might be one of those things that varies with AS versions, and
>
the test is simplistic, but the difference between the first two times is
>
pretty dramatic.
There's a similar, but far less dramatic difference between 'characters
of x' and 'length of characters of x'. 'Number of characters of x' goes
in the opposite direction, speedwise.
'Count' can also take a singular parameter, giving all these choices for
getting the same result from it (timings for 10000 iterations on my 4400,
Mac OS 8.6):
count x 47 ticks
count x each character 50 ticks
count character of x 65 ticks -- These two take turns
count characters of x 67 ticks -- at being the faster
count each character of x 76 ticks
It also lead to this little curiosity:
set x to {"a", 2, "c", 4, "e"}
{number of x, count number of x}
--> {5, 2}
NG