Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Getting current time in milliseconds

Reply
Thread Tools

Getting current time in milliseconds

 
 
laredotornado
Guest
Posts: n/a
 
      10-20-2009
Hi,

I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
sh), do you know how I could use perl to get the current time in
milliseconds (or microseconds) and store the value in a shell
variable?

Thanks, - Dave
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      10-20-2009
In article
<(E-Mail Removed)>,
laredotornado <(E-Mail Removed)> wrote:

> Hi,
>
> I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
> sh), do you know how I could use perl to get the current time in
> milliseconds (or microseconds) and store the value in a shell
> variable?


% set t=`perl -MTime::HiRes=gettimeofday -e 'print
int(1000*gettimeofday()).qq(\n);'` ; echo Time in msec is $t
Time in msec is 1256077820486
%

--
Jim Gibson
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-21-2009
laredotornado <(E-Mail Removed)> wrote:
>I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
>sh), do you know how I could use perl to get the current time in
>milliseconds (or microseconds)


perldoc Time::HiRes

> and store the value in a shell variable?


That is impossible because child processes cannot alter variables of the
parent process. See "perldoc -q env" for a possible workaround.

jue
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-21-2009
Jürgen Exner <(E-Mail Removed)> writes:
> laredotornado <(E-Mail Removed)> wrote:
>>I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
>>sh), do you know how I could use perl to get the current time in
>>milliseconds (or microseconds)

>
> perldoc Time::HiRes
>
>> and store the value in a shell variable?

>
> That is impossible because child processes cannot alter variables of the
> parent process. See "perldoc -q env" for a possible workaround.


But a child process can certainly modify its own environment in
response to the output of something it invokes.

The workaround suggested in "perldoc -q env" requires the Perl script
to generate a shell command to be evaluated, but that's not necessary.
For example:

#!/bin/sh
curr_time=$(perl -e "...")

where the Perl command just prints the time.

Note that the older (and still supported) form of $(command) is
`command`. csh and tcsh support `command` but not $(command);
I don't know if any sh derivatives fail to support $(command).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      10-21-2009
Keith Thompson <(E-Mail Removed)> wrote:
>Jürgen Exner <(E-Mail Removed)> writes:
>> laredotornado <(E-Mail Removed)> wrote:
>>>I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
>>>sh), do you know how I could use perl to get the current time in
>>>milliseconds (or microseconds)

>>
>> perldoc Time::HiRes
>>
>>> and store the value in a shell variable?

>>
>> That is impossible because child processes cannot alter variables of the
>> parent process. See "perldoc -q env" for a possible workaround.

>
>But a child process can certainly modify its own environment in
>response to the output of something it invokes.


But then it is acting as a new parent process, not as a child process.
If you want to split hairs, then I should rephrase my answer as:

>>> and store the value in a shell variable?


You set the value in the hash %ENV. But that doesn't do you any good
because it sets the value in the currently running Perl process, not in
the calling shell process. If you want to modify the value of the shell
variable in a parent process directly from the child process than it
cannot be done.

>The workaround suggested in "perldoc -q env" requires the Perl script
>to generate a shell command to be evaluated, but that's not necessary.

[example snipped]

True, but all of that is some magic on the shell side and applies to any
process called by the shell in any programming language and therefore
doesn't have anything to do with Perl and that's why the answer in the
Perl FAQ points you to the documentation and FAQ for whatever shell you
are using.

jue
 
Reply With Quote
 
laredotornado
Guest
Posts: n/a
 
      10-21-2009
On Oct 20, 4:31*pm, Jim Gibson <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
>
> laredotornado<(E-Mail Removed)> wrote:
> > Hi,

>
> > I'm on a Mac OS 10.5.6 with Perl 5.8.8. *From a shell script (/bin/
> > sh), do you know how I could use perl to get the current time in
> > milliseconds (or microseconds) and store the value in a shell
> > variable?

>
> % set t=`perl -MTime::HiRes=gettimeofday -e 'print
> int(1000*gettimeofday()).qq(\n);'` ; echo Time in msec is $t
> Time in msec is 1256077820486
> %
>
> --
> Jim Gibson


Thanks, Jim. That's a winner! - Dave
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-22-2009
Jürgen Exner <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> wrote:
>>Jürgen Exner <(E-Mail Removed)> writes:
>>> laredotornado <(E-Mail Removed)> wrote:
>>>>I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
>>>>sh), do you know how I could use perl to get the current time in
>>>>milliseconds (or microseconds)
>>>
>>> perldoc Time::HiRes
>>>
>>>> and store the value in a shell variable?
>>>
>>> That is impossible because child processes cannot alter variables of the
>>> parent process. See "perldoc -q env" for a possible workaround.

>>
>>But a child process can certainly modify its own environment in
>>response to the output of something it invokes.

>
> But then it is acting as a new parent process, not as a child process.


Whoops, you're right, I meant "parent process", not "child process".

[...]

>>The workaround suggested in "perldoc -q env" requires the Perl script
>>to generate a shell command to be evaluated, but that's not necessary.

> [example snipped]
>
> True, but all of that is some magic on the shell side and applies to any
> process called by the shell in any programming language and therefore
> doesn't have anything to do with Perl and that's why the answer in the
> Perl FAQ points you to the documentation and FAQ for whatever shell you
> are using.


The Perl FAQ points to a particular shell-specific solution, using
"eval" (that's the shell's "eval", not Perl's "eval").

If it's going to avoid shell-specific solutions, it shouldn't mention
"eval". If not, it should mention some better solution.

(And yes, having a Perl script print a shell command that can then be
eval'ed by an invoking shell can be useful sometimes, but a simple
"x=$(./perl-script)" usually works just as well, and more safely.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      10-22-2009
On Oct 21, 9:30*am, laredotornado <(E-Mail Removed)> wrote:
> On Oct 20, 4:31*pm, Jim Gibson <(E-Mail Removed)> wrote:
>
>
>
> > In article
> > <(E-Mail Removed)>,

>
> > laredotornado<(E-Mail Removed)> wrote:
> > > Hi,

>
> > > I'm on a Mac OS 10.5.6 with Perl 5.8.8. *From a shell script (/bin/
> > > sh), do you know how I could use perl to get the current time in
> > > milliseconds (or microseconds) and store the value in a shell
> > > variable?

>
> > % set t=`perl -MTime::HiRes=gettimeofday -e 'print
> > int(1000*gettimeofday()).qq(\n);'` ; echo Time in msec is $t
> > Time in msec is 1256077820486
> > %

>
> > --
> > Jim Gibson

>
> Thanks, Jim. *That's a winner! - Dave



Depends on your definition of 'current time'.

time set t=`perl -MTime::HiRes=gettimeofday -e 'print int
(1000*gettimeofday()).qq(\n);'`

real 0m0.041s
user 0m0.032s
sys 0m0.009s


As long as you aren't expecting microsecond overhead
this is fine. If you really want to do times with
millisecond resolution, starting up a perl instance
is probably not the right way to go.
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      10-22-2009

smallpond wrote:
> On Oct 21, 9:30 am, laredotornado <(E-Mail Removed)> wrote:
>> On Oct 20, 4:31 pm, Jim Gibson <(E-Mail Removed)> wrote:
>>
>>
>>
>>> In article
>>> <(E-Mail Removed)>,
>>> laredotornado<(E-Mail Removed)> wrote:
>>>> Hi,
>>>> I'm on a Mac OS 10.5.6 with Perl 5.8.8. From a shell script (/bin/
>>>> sh), do you know how I could use perl to get the current time in
>>>> milliseconds (or microseconds) and store the value in a shell
>>>> variable?
>>> % set t=`perl -MTime::HiRes=gettimeofday -e 'print
>>> int(1000*gettimeofday()).qq(\n);'` ; echo Time in msec is $t
>>> Time in msec is 1256077820486
>>> %
>>> --
>>> Jim Gibson

>> Thanks, Jim. That's a winner! - Dave

>
>
> Depends on your definition of 'current time'.
>
> time set t=`perl -MTime::HiRes=gettimeofday -e 'print int
> (1000*gettimeofday()).qq(\n);'`
>
> real 0m0.041s
> user 0m0.032s
> sys 0m0.009s
>
>
> As long as you aren't expecting microsecond overhead
> this is fine. If you really want to do times with
> millisecond resolution, starting up a perl instance
> is probably not the right way to go.


<nit pick>

s/resolution/accuracy/

http://www.tutelman.com/golf/measure/precision.php

</nit pick>

--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
time in milliseconds by calling time.time() scriptlearner@gmail.com Python 3 07-25-2009 06:18 AM
Current Time with 5 digits of milliseconds ambarish.mitra@gmail.com Perl Misc 6 05-12-2008 06:52 PM
How do I display the current time *including milliseconds*? Al Koch Java 5 07-02-2005 01:26 AM
Newbie Question: How to return the current time in seconds or milliseconds? Michelle Java 5 08-17-2004 05:44 AM
getting elapsed time in milliseconds Thomas Baier C++ 6 09-22-2003 01:51 AM



Advertisments