Getting current time in milliseconds

Discussion in 'Perl Misc' started by laredotornado, Oct 20, 2009.

  1. 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
     
    laredotornado, Oct 20, 2009
    #1
    1. Advertising

  2. laredotornado

    Jim Gibson Guest

    In article
    <>,
    laredotornado <> 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
     
    Jim Gibson, Oct 20, 2009
    #2
    1. Advertising

  3. laredotornado <> 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
     
    Jürgen Exner, Oct 21, 2009
    #3
  4. Jürgen Exner <> writes:
    > laredotornado <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 21, 2009
    #4
  5. Keith Thompson <> wrote:
    >Jürgen Exner <> writes:
    >> laredotornado <> 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
     
    Jürgen Exner, Oct 21, 2009
    #5
  6. On Oct 20, 4:31 pm, Jim Gibson <> wrote:
    > In article
    > <>,
    >
    > laredotornado<> 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
     
    laredotornado, Oct 21, 2009
    #6
  7. Jürgen Exner <> writes:
    > Keith Thompson <> wrote:
    >>Jürgen Exner <> writes:
    >>> laredotornado <> 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) <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"
     
    Keith Thompson, Oct 22, 2009
    #7
  8. laredotornado

    smallpond Guest

    On Oct 21, 9:30 am, laredotornado <> wrote:
    > On Oct 20, 4:31 pm, Jim Gibson <> wrote:
    >
    >
    >
    > > In article
    > > <>,

    >
    > > laredotornado<> 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.
     
    smallpond, Oct 22, 2009
    #8
  9. smallpond wrote:
    > On Oct 21, 9:30 am, laredotornado <> wrote:
    >> On Oct 20, 4:31 pm, Jim Gibson <> wrote:
    >>
    >>
    >>
    >>> In article
    >>> <>,
    >>> laredotornado<> 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
     
    RedGrittyBrick, Oct 22, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michelle
    Replies:
    5
    Views:
    4,265
    Michelle
    Aug 17, 2004
  2. Al Koch
    Replies:
    5
    Views:
    11,271
    James Korman
    Jul 2, 2005
  3. Thomas Baier

    getting elapsed time in milliseconds

    Thomas Baier, Sep 20, 2003, in forum: C++
    Replies:
    6
    Views:
    24,997
    J. Campbell
    Sep 22, 2003
  4. Replies:
    3
    Views:
    2,022
    Steven D'Aprano
    Jul 25, 2009
  5. Replies:
    6
    Views:
    3,451
    John W. Krahn
    May 12, 2008
Loading...

Share This Page