Re: Getting time components of date
Re: Getting time components of date
- Subject: Re: Getting time components of date
- From: Graff <email@hidden>
- Date: Tue, 23 Dec 2003 04:51:49 -0500
Nah, I didn't bother to time it. I figured that any slowdown would be
due to using the "do shell script" command to call the command-line
tool. I was just being cheeky and offering the "ultimate fastest"
solution.
I have no doubt that Arthur's script is quick but sometimes you need to
balance speed verses complexity. The best balance of speed and
complexity is probably the command-line tool "date" even though it is
0.0076 seconds slower than Arthur's code. I mean, to use the date tool
you need 1 line of code as opposed to 123 for Arthur's code, even with
the comments and extra blank lines taken out of it!
Anyways, if this function really needs to be any faster than 0.01
seconds then AppleScript is the wrong tool to be using for this
purpose.
:-)
- Ken
On Dec 23, 2003, at 3:52 AM, Paul Berkowitz wrote:
But did you test it against Arthur's script?
The reason why some pure AppleScripts can be faster than a 'do shell
script', no matter how fast the actual shell script is in Terminal, is
that
'do shell script' is a scripting addition and, as such, has an
overhead when
run in AppleScript. AppleScript first searches the 'current
application' for
all terms, then every scripting addition until it hits 'do shell
script',
then has to hook into sh, and so on. If you take even a cursory look at
Arthur's handler you'll see that it almost all consists of 'else if'
branchings, so only a tiny portion of it actually runs.
I haven't tested it either, but I'm sure someone will.
From: Graff <email@hidden>
Date: Tue, 23 Dec 2003 03:23:02 -0500
Well if we are talking speed, you probably won't get faster than this
unless you like to code in machine code:
-------------
#include <stdio.h>
#include <time.h>
#define MAX_SIZE 32
#define FORMAT_STRING "%Y-%m-%d %H:%M:%S\n"
int main (int argc, const char * argv[])
{
time_t *currTime;
struct tm *currLocalTime;
char outputString[MAX_SIZE];
time(currTime);
currLocalTime = localtime(currTime);
if (currLocalTime != NULL)
{
strftime(outputString, MAX_SIZE - 1, FORMAT_STRING, currLocalTime);
}
printf("%s", outputString);
return 0;
}
-------------
Put it into a text editor, save it on your desktop as a plain text
file
with unix line endings and the name "timestring.c". Go into the
terminal and type:
cc ~/Desktop/timestring.c -o ~/Desktop/timestring
Boom, your very own command-line tool. Run it in AppleScript like
this:
set theTime to do shell script "~/Desktop/timestring"
Don't mind me, I'm feeling a little cheeky. ;-)
- Ken
On Dec 23, 2003, at 12:02 AM, Christopher Stone wrote:
At 7:59 AM -0500 12/22/03, Chris Garaffa wrote:
While working to convert an AppleScript date to a MySQL DATETIME
date
(yyyy-mm-dd hh:mm:ss) I ran into a small problem. While AppleScript
supports things like:
_____________________________________________________________________
_
Here's Arthur Knapp's handler. On my system it creates the requisite
date string in ~ 0.0024 seconds.
As opposed to ~ 0.01 seconds for the shell script.
_______________________________________________
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.