From: Lao Ming on
On Oct 20, 3:54 pm, Glenn Jackman <gle...(a)ncf.ca> wrote:
> At 2009-10-20 05:23PM, "laredotornado" wrote:
>
> >  Hi,
>
> >  In a shell script (sh), how do I store the time in milliseconds (or
> >  microseconds, don't care) to a variable?
>
> Not in the shell, but with per:
>
>    perl -MTime::HiRes -e 'print int(1000 * Time::HiRes::gettimeofday),"\n"'
>
> The overhead of invoking perl will certainly add a few milleseconds...
>
> --
> Glenn Jackman
>     Write a wise saying and your name will live forever. -- Anonymous

About 22-23 milliseconds for the overhead of each iteration below:

#! /bin/bash
i=0
while [ $i -le 60 ]
do
ms=$( perl -MTime::HiRes -e 'print int(1000 *
Time::HiRes::gettimeofday),"\n"' )
sec=$( date '+%S' )
printf '%2d %d\n' "$sec" "$ms"
sleep 1
i=$(( $i + 1 ))
done

I'm sure there are better ways to do this so feel free.
From: mop2 on
On Tue, 20 Oct 2009 19:23:53 -0200, laredotornado <laredotornado(a)zipmail.com> wrote:

> In a shell script (sh), how do I store the time in milliseconds (or
> microseconds, don't care) to a variable?


nano seconds since 1970-01-01 00:00:00 UTC:

$ date --version|head -n1
date (GNU coreutils) 7.6

$ NS=`date +%s%N`


$ echo $NS
1256090733975302128
From: John DuBois on
In article <0bp1r6x914.ln2(a)goaway.wombat.san-francisco.ca.us>,
Keith Keller <kkeller-usenet(a)wombat.san-francisco.ca.us> wrote:
>On 2009-10-20, laredotornado <laredotornado(a)zipmail.com> wrote:
>> On Oct 20, 3:48�pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
>> wrote:
>>>
>>> Depending on what shell the 'sh' actually is, in a modern ksh93 you can do
>>>
>>> � �ms=${SECONDS#*.}
>>>
>>> (For plain sh Bourne shells you'd have to rely on external programs.)
>>
>> I meant milliseconds/microseconds from the actual time, yes.
>> I'm on a Mac OS 10.5.6. Here is what I see in response to uname -a:
>
>I don't think that ksh code will give you what you want anyway; if my
>reading of the man page is right, it gives you the number of seconds
>since shell invocation, not absolute milliseconds.

Yes, that's correct.

This will give current nanoseconds, on new-enough ksh93:

ns=$(printf "%(%N)T")

John
--
John DuBois spcecdt(a)armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/