Getting current time in milliseconds

L

laredotornado

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
 
J

Jim Gibson

laredotornado said:
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
%
 
J

Jürgen Exner

laredotornado said:
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
 
K

Keith Thompson

Jürgen Exner said:
perldoc Time::HiRes


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).
 
J

Jürgen Exner

Keith Thompson said:
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:

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
 
L

laredotornado

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

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

Keith Thompson

Jürgen Exner said:
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.)
 
S

smallpond

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.
 
R

RedGrittyBrick

smallpond said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top