• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: monthNumber (was: Create a folder with today's date...)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: monthNumber (was: Create a folder with today's date...)


  • Subject: Re: monthNumber (was: Create a folder with today's date...)
  • From: Richard Morton <email@hidden>
  • Date: Wed, 13 Feb 2002 10:17:03 +1100

On Tuesday, February 12, 2002, at 02:06 AM, has wrote:
Richard Morton wrote:

[FV vs ifThen]
If you speed tested these 2 handlers as posted, you're comparing ice
cream to custard. ;-} GetMonthNumber() includes a call to 'current
date', whereas your handler gets the month already supplied.
Whatever you did, I get significantly different results.

No, no, was a fair test, one hand tied behind my back, etc. ;)

And the other on the scales perhaps? ;-P No, it sounds like it's due to differences in AS versions from what Nigel has posted here since. That does surprise me I must say, but I guess life would be a bit boring without surprises.


Here's a fresh comparison, using an if...elseif block...

...Now, it beats FV even in the worst case (December), at least for me. It
may be that other folks' mileage varies according to AS version...

--> {150, 373} (January)
--> {329, 380} (December)

I ran that exact code & got results that were reasonably consistent with my original assertion:

--> {197, 492} (January)
--> {928, 493} (December)

Very different to yours. My "hunky G4" also took a lot longer than whatever you're running.


Putting it into 1 if statement is faster, as I expected, but not by much.
It nearly doubles the line count too.

Hey, looks aren't everything you know. Personality goes a long way too. ;)

Sure, but no matter what anyone says - size does matter. ;-) I speed tested all these things again yesterday & again realised that somebody would have to come up with a dramatically faster method of getting, say, a month number, to make any appreciable difference to the overall execution time of a short date handler. Even if somebody did, it's only going to help a person who's calling it thousands of times.

I wrote a few new handlers after testing and the difference between the fastest one I could write and the most compact was very small in terms of speed - maybe 10% or so. This might actually matter if it was taking a long time, but it's not. These things are so fast that such things as multiple variable declarations on one line or getting the month number in a separate handler, actually make a difference:

set {a, b, c} to {1, 2, 3}

is measurably slower than:

set a to 1
set b to 2
set c to 3

FWIW, I've noted that calling a handler has about the same overhead (on Hunky) as declaring 2 variables on 1 line. 3 variables, as above, takes longer again.

These 2 return the date in my preferred format - 13Feb2002 - and both require the property below. Declaring this list within the handler adds significantly to time.

property sMonList : {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

-- fastest I can come up with (requires 'sMonList' property)
--> sessionAvg:0.0413, sessionBest:0.0379, sessionWorst:0.0536 (day=1)
--> sessionAvg:0.0389, sessionBest:0.0358, sessionWorst:0.0531 (day=10)
--> average for month=0.0396
to shortDateFastest from aDate
copy aDate to cDate
set month of cDate to January
set dd to aDate's day
if dd < 10 then set dd to ("0" & dd)
return (dd & sMonList's item (1 + (aDate - cDate) div 2.5E+6) & year of aDate) as string
end shortDateFastest

-- most compact (requires 'sMonList' & 'monthNum')
--> sessionAvg:0.0444, sessionBest:0.0407, sessionWorst:0.0559 (any day)
to shortDateMostCompact from aDate
return text 2 thru 3 of ((100 + (aDate's day)) as string) & sMonList's item (monthNum from aDate) & year of aDate -- all one line
end shortDateMostCompact

on monthNum from dateObj -- called by 'shortDateMostCompact'
copy dateObj to dateTemp
set month of dateTemp to January
return (1 + (dateObj - dateTemp) div 2.5E+6)
end monthNum


Note that 'shortDateMostCompact' uses the very cool "Bangers & Mash" (sorry Nigel ;-) method for adding leading zeroes:

text 2 through 3 of ((100 + <singleDigitNumber>) as string)


...1/2/3 - an ingenious method for returning date strings in
the order the user has set in his/her Date & Time CP...

I might have seen this, I can't recall. Interesting idea, though as folks
have maybe gathered I'm not a real big fan of AS's dependence on the D&T
CP
for formatting - it makes it trickier to write machine-independent code as
you've got to be very careful what you do with dates as strings.

Sure, but at least if Joe or Jane New-User ask for a date, they'll get it back in the format they expect. I think that's a good thing. People like us can deal with this sort of stuff far more easily than they can.


In the meantime, I just patch
the gap for myself. (One of these days I might even come up with a version
of dateLib that I'm actually happy with and release the blighter...;)

Good luck. :-} I had a go at it sometime ago & came to the conclusion that AS is too slow and clunky to allow the sort of flexibility that an osax can. AS can get seriously bogged down speedwise by complex conditional structures. We also don't have the capacity to use optional parameters when writing handlers which means that it's virtually impossible to create a simple interface to something that has lots of options.

I'm sure it would be different for others, but I have no interest in writing, or using, a handler that requires me to spend a minute or two studying its documentation every time I want to call it. This has led me to focus more on small sharp tools, which, as we've seen here, can be very efficient.

Anyway, a search of my shortDate vault revealed the handler below, which I thought some people might find interesting. It was posted here (or on macscrpt) by John Delacour, probably a couple of years ago. ShortDate afficionados will note it's ancestry. I didn't test it for speed.

on getDate(d, sep, zeros, century, yearFirst)
set {d1, aDay} to {d, 1314864}
copy d1 to d2
set d2's month to January
set y to d1's year as string
set d to d1's day
set m to 1 + (d1 - d2 + aDay) div (aDay * 2)
if zeros then
if m < 10 then set m to "0" & m
if d < 10 then set d to "0" & d
end if
if not century then set y to text 3 through -1 of y
if yearFirst then return y & sep & m & sep & d
"" & m & sep & d & sep & y
end getDate
getDate(current date, "-", true, false, false)


Cheers,

Richard Morton
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Exit Repeat [Was: [Case Statements]]
  • Next by Date: Re: Exit Repeat [Was: [Case Statements]]
  • Previous by thread: Re: monthNumber (was: Create a folder with today's date...)
  • Next by thread: Re: monthNumber (was: Create a folder with today's date...)
  • Index(es):
    • Date
    • Thread