Re: elapsedTime - one mo' time
Re: elapsedTime - one mo' time
- Subject: Re: elapsedTime - one mo' time
- From: Richard Morton <email@hidden>
- Date: Wed, 9 Jan 2002 11:57:00 +1100
On Tuesday, January 8, 2002, at 03:44 AM, Nigel Garvey wrote:
-- elapsedTime -- Richard Morton 2001-2002 --
As you can see, it took me 2 years to write that...
Hopefully Nigel will let us know if it's not fast enough...
Harrummphh! :-)
...and you, but a few short hours to improve on it. <sigh>
Not even I would bother with the miniscule speed advantage to be gained
from using integers instead of 'hours' and 'days'!
;-P I was desperate. No, actually, it never occurred to me to use the
constants. That's very neat. Makes it more friendly looking too.
However, I might be
tempted to reverse the flow of the handler so as not to have to bother
with the variables 'iRes' and 'fMac', and to use a slightly faster
leading-zero method that I've just invented in your honour:
Well now! That _is_ an honour! I'm lost for words...
tell secondTime
text -2 thru -1 of ((100 + it mod hours div minutes) as
string) & s
This is very cool you know, but I'm only giving you a gold star and an
elephant stamp because it's so much more compact & readable. You're
slipping a bit in the speed stakes, by my reckoning:
elapsedTime from n against ":" -- as originally posted
--> count of characters=595
--> sessionAvg:0.00048, sessionBest:0.00037, sessionWorst:0.00119 (n =
360)
--> sessionAvg:0.00062, sessionBest:0.00049, sessionWorst:0.0015 (n =
3600)
--> sessionAvg:0.00073, sessionBest:0.00061, sessionWorst:0.00106 (n =
360000)
elapsedTimeNG1A from n against ":" -- Nigel's first one, using "text -2
though -1..."
--> count of characters=536
--> sessionAvg:0.00042, sessionBest:0.00034, sessionWorst:0.00083 (n =
360)
--> sessionAvg:0.00057, sessionBest:0.00045, sessionWorst:0.00134 (n =
3600)
--> sessionAvg:0.00068, sessionBest:0.00056, sessionWorst:0.00149 (n =
360000)
elapsedTimeNG1B from n against ":" -- as above, using "text 2 through
3..."
--> count of characters=526
--> sessionAvg:0.00042, sessionBest:0.00033, sessionWorst:0.00123 (n =
360)
--> sessionAvg:0.00054, sessionBest:0.00044, sessionWorst:0.00125 (n =
3600)
--> sessionAvg:0.00067, sessionBest:0.00053, sessionWorst:0.00169 (n =
360000)
A mere 12% faster, at best. ;-P I'm presuming you get similar
results..?
The surgery needed to adapt this to produce an 'hours' figure even when
that's zero is a little more profound:
Worth the effort though. Even with a little bug in the days figure:
if it is not less than days then
text -2 thru -1 of ((100 + it div hours) as string) & s & result
This normally means that you would only get a silver star, but we'll
overlook it in this case ;-} It's got to be the winner, surely:
elapsedTimeNG2B from n against ":" -- the 2nd one, using "text 2
through 3..."
--> count of characters=419
--> sessionAvg:0.00053, sessionBest:0.00043, sessionWorst:0.00134 (n =
360)
--> sessionAvg:0.00052, sessionBest:0.00043, sessionWorst:0.00085 (n =
3600)
--> sessionAvg:0.00065, sessionBest:0.00053, sessionWorst:0.00152 (n =
360000)
It loses the speed advantage on the smallest number of course, but,
considerably smaller _and_ significantly faster is a beautiful thing in
my book.
In case anyone's interested in these speed figures, the 3600 & 360000
results for each variation on Nigel's handlers are pretty much
identical. In other words, I get that sort of variation testing the
same handler 3 times. That said, my hunch (based on the 'sessionBest'
times) is that it is faster to use "text 2 through 3..." in this
situation, but only by a very small amount.
Here then is a slightly more compacted & debugged version:
on elapsedTime from secondTime against s
tell secondTime
set hms to text 2 thru 3 of ((100 + it mod days div hours) as
string) & s & [Option-L]
text 2 thru 3 of ((100 + it mod hours div minutes) as
string) & s & [Option-L]
text 2 thru 3 of ((100 + it mod minutes as integer) as string)
if it is not less than days then return text 2 thru 3 of
((100 + it div days) as string) & s & hms
return hms
end tell
end elapsedTime
It's about the same speed and character count, just a few less lines.
Cheers,
Richard
-- From the Vault of Road Crew Wisdom:
"If women are from Venus and men are from Mars,
then drummers are from Pluto."