How to calculate elapsed time ?

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default How to calculate elapsed time ?

    Hi all:

    I'm trying to determine how long an system operation takes. Anyone know
    of a simple way to do this ?

    I wanted to establish the start time. Then run the operation. Then mark
    the finish time. Then substract the start time from the finish time to
    get an elapsed time. Here's the simplistic approach I tried. I'm sure I
    need a time that is measured in seconds or something like that, but I'm
    not sure how to do this.

    TIA

    Here's what I tried:

    #!/usr/bin/perl -w
    use warnings;
    use strict;

    my $start = "Tue Jan 27 15:40:16 2004";
    print "This is the start time: $start \n";

    system (This is where the system process stuff goes);

    my $finish = localtime;
    print "This is the finish time: $finish \n";

    my $elapsedtime = ("$finish" - "$start") ;
    print "This is the time diff: $elapsedtime \n";

    The above obviously didn't work. Here's what it returned:

    This is the start time: Tue Jan 27 15:40:16 2004
    Argument "Tue Jan 27 15:40:16 2004" isn't numeric in subtraction (-) at
    C:\Perl\timetest.pl line 13.
    This is the finish time: Tue Jan 27 19:45:56 2004
    This is the time diff: 0
    Argument "Tue Jan 27 19:45:56 2004" isn't numeric in subtraction (-) at
    C:\Perl\timetest.pl line ClearCase\Us
    Stuart Clemons Guest

  2. Similar Questions and Discussions

    1. Time elapsed record
      Running FM 5.5 on PC. Is there a way to show elapsed times for each record in table view. I want to show how long a patient has been admitted to...
    2. Elapsed time record help.
      Running FM 5.5 on PC. Is there a way to show elapsed times for each record in table view. I want to show how long a patient has been admitted to...
    3. Calculate elapsed time in minutes
      Try: DateDiff("n",,) Your code has you comparing TaskStart to TaskStart and then dividing the result (which is in minutes from the 'n'...
    4. Calculate Time
      I have a form that shows the Employees loginTime and logoffTime with a medium time format. The Employee clocks in and then out. I need to...
    5. calculate the time
      Personal, necessary to do a select that contains for NAME, if the name has more than a connection I want to catch the total time of those...
  3. #2

    Default Re: How to calculate elapsed time ?

    On Jan 27, 2004, at 6:50 PM, [email]stuart_clemons@us.ibm.com[/email] wrote:
    > Hi all:
    Howdy.
    > I'm trying to determine how long an system operation takes. Anyone
    > know
    > of a simple way to do this ?
    You were on the right track. Try this:

    my $start = time;

    # do something to time here

    my $time_taken = time - $start;

    If you want more information on the time() subroutine, try (at the
    commandline):

    perldoc -f time

    And if you get into timing things it won't be long before you want the
    Benchmarking module, generally. You can read about it too:

    perldoc Benchmark

    Hope that helps.

    James

    James Edward Gray II Guest

  4. #3

    Default Re: How to calculate elapsed time ?

    On 1/27/2004 7:50 PM, [email]stuart_clemons@us.ibm.com[/email] wrote:
    > Hi all:
    >
    > I'm trying to determine how long an system operation takes. Anyone know
    > of a simple way to do this ?
    >
    > I wanted to establish the start time. Then run the operation. Then mark
    > the finish time. Then substract the start time from the finish time to
    > get an elapsed time. Here's the simplistic approach I tried. I'm sure I
    > need a time that is measured in seconds or something like that, but I'm
    > not sure how to do this.
    Use the Benchmark module or Time::Hires.

    Randy.


    Randy W. Sims Guest

  5. #4

    Default RE: How to calculate elapsed time ?


    > -----Original Message-----
    > From: [email]stuart_clemons@us.ibm.com[/email] [mailto:stuart_clemons@us.ibm.com]
    > Sent: Wednesday, January 28, 2004 11:51 AM
    > To: [email]beginners@perl.org[/email]
    > Subject: How to calculate elapsed time ?
    >
    >
    > Hi all:
    >
    > I'm trying to determine how long an system operation takes.
    > Anyone know
    > of a simple way to do this ?
    >
    > I wanted to establish the start time. Then run the
    > operation. Then mark
    > the finish time. Then substract the start time from the
    > finish time to
    > get an elapsed time. Here's the simplistic approach I tried.
    > I'm sure I
    > need a time that is measured in seconds or something like
    > that, but I'm
    > not sure how to do this.
    >
    > TIA
    >
    [code snipped]
    >
    use Time::Elapse;

    # somewhere in your program...
    Time::Elapse->lapse(my $now);

    #...rest of program execution

    print "Time Wasted: $now\n";

    Toby Stuart Guest

  6. #5

    Default Re: How to calculate elapsed time ?

    [email]stuart_clemons@us.ibm.com[/email] wrote:
    > Hi all:
    >
    > I'm trying to determine how long an system operation takes. Anyone know
    > of a simple way to do this ?
    >
    > I wanted to establish the start time. Then run the operation. Then mark
    > the finish time. Then substract the start time from the finish time to
    > get an elapsed time. Here's the simplistic approach I tried. I'm sure I
    > need a time that is measured in seconds or something like that, but I'm
    > not sure how to do this.
    >
    > TIA
    >
    > Here's what I tried:
    >
    > #!/usr/bin/perl -w
    > use warnings;
    > use strict;
    >
    > my $start = "Tue Jan 27 15:40:16 2004";
    > print "This is the start time: $start \n";
    >
    > system (This is where the system process stuff goes);
    >
    > my $finish = localtime;
    > print "This is the finish time: $finish \n";
    >
    > my $elapsedtime = ("$finish" - "$start") ;
    > print "This is the time diff: $elapsedtime \n";
    >
    > The above obviously didn't work. Here's what it returned:
    >
    > This is the start time: Tue Jan 27 15:40:16 2004
    > Argument "Tue Jan 27 15:40:16 2004" isn't numeric in subtraction (-) at
    > C:\Perl\timetest.pl line 13.
    > This is the finish time: Tue Jan 27 19:45:56 2004
    > This is the time diff: 0
    > Argument "Tue Jan 27 19:45:56 2004" isn't numeric in subtraction (-) at
    > C:\Perl\timetest.pl line ClearCase\Us
    Assuming you don't need more granularity than seconds, you should just
    use 'time'. Don't hardcode the start time either as this seems kind of
    pointless, something like:

    my $start = time;

    system();

    my $end = time;

    Then subtract the two, that returns # of seconds. perldoc -f time

    Depending on what your 'system' is doing you may need to look into
    Time::HiRes to get partial second granularity.

    [url]http://danconia.org[/url]
    Wiggins D'Anconia Guest

  7. #6

    Default Re: How to calculate elapsed time ?

    Stuart Clemons wrote:
    >
    > Hi all:
    Hello,
    > I'm trying to determine how long an system operation takes. Anyone know
    > of a simple way to do this ?
    Certainly. :-)
    > I wanted to establish the start time. Then run the operation. Then mark
    > the finish time. Then substract the start time from the finish time to
    > get an elapsed time. Here's the simplistic approach I tried. I'm sure I
    > need a time that is measured in seconds or something like that, but I'm
    > not sure how to do this.
    Hint:
    perldoc -f time

    > Here's what I tried:
    >
    > #!/usr/bin/perl -w
    > use warnings;
    > use strict;
    >
    > my $start = "Tue Jan 27 15:40:16 2004";
    > print "This is the start time: $start \n";
    my $start = time;
    print 'This is the start time: ', scalar localtime $start, "\n";
    > system (This is where the system process stuff goes);
    >
    > my $finish = localtime;
    > print "This is the finish time: $finish \n";
    my $finish = time;
    print 'This is the finish time: ', scalar localtime $finish, "\n";

    > my $elapsedtime = ("$finish" - "$start") ;
    > print "This is the time diff: $elapsedtime \n";
    my $elapsedtime = $finish - $start;
    print "This is the time diff in seconds: $elapsedtime\n";


    If you need microseconds instead of seconds look at the Time::HiRes
    module.

    perldoc Time::HiRes

    Also have a look at the Benchmark module.

    perldoc Benchmark



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  8. #7

    Default Re: How to calculate elapsed time ?


    On Tue, 27 Jan 2004 [email]stuart_clemons@us.ibm.com[/email] wrote:
    > I'm trying to determine how long an system operation takes. Anyone know
    > of a simple way to do this ?
    >
    > I wanted to establish the start time. Then run the operation. Then mark
    > the finish time. Then substract the start time from the finish time to
    > get an elapsed time. Here's the simplistic approach I tried. I'm sure I
    > need a time that is measured in seconds or something like that, but I'm
    > not sure how to do this.

    You might want to look $^T variable.

    This variable is the time (secs from 1970) that a perl program is started.



    Owen

    Owen Cook Guest

  9. #8

    Default Re: How to calculate elapsed time ?

    On Jan 27, 2004, at 6:50 PM, [email]stuart_clemons@us.ibm.com[/email] wrote:
    > Hi all:
    Howdy.
    > I'm trying to determine how long an system operation takes. Anyone
    > know
    > of a simple way to do this ?
    You were on the right track. Try this:

    my $start = time;

    # do something to time here

    my $time_taken = time - $start;

    If you want more information on the time() subroutine, try (at the
    commandline):

    perldoc -f time

    And if you get into timing things it won't be long before you want the
    Benchmarking module, generally. You can read about it too:

    perldoc Benchmark

    Hope that helps.

    James

    James Edward Gray II Guest

  10. #9

    Default Re: How to calculate elapsed time ?

    Thanks guys. You're the Best !

    I used "time" instead of "localtime" and it's exactly what I need for now.
    Perl is quickly becoming a very useful tool for me.

    Thanks also for the doc info: perldoc -f time & perldoc Benchmark

    - Stuart

    Stuart Clemons/Westford/IBM wrote on 01/27/2004 07:50:30 PM:
    >
    > Hi all:
    >
    > I'm trying to determine how long an system operation takes. Anyone
    > know of a simple way to do this ?
    >
    > I wanted to establish the start time. Then run the operation. Then
    > mark the finish time. Then substract the start time from the
    > finish time to get an elapsed time. Here's the simplistic approach
    > I tried. I'm sure I need a time that is measured in seconds or
    > something like that, but I'm not sure how to do this.
    >
    > TIA
    >
    > Here's what I tried:
    >
    > #!/usr/bin/perl -w
    > use warnings;
    > use strict;
    >
    > my $start = "Tue Jan 27 15:40:16 2004";
    > print "This is the start time: $start \n";
    >
    > system (This is where the system process stuff goes);
    >
    > my $finish = localtime;
    > print "This is the finish time: $finish \n";
    >
    > my $elapsedtime = ("$finish" - "$start") ;
    > print "This is the time diff: $elapsedtime \n";
    >
    > The above obviously didn't work. Here's what it returned:
    >
    > This is the start time: Tue Jan 27 15:40:16 2004
    > Argument "Tue Jan 27 15:40:16 2004" isn't numeric in subtraction (-)
    > at C:\Perl\timetest.pl line 13.
    > This is the finish time: Tue Jan 27 19:45:56 2004
    > This is the time diff: 0
    > Argument "Tue Jan 27 19:45:56 2004" isn't numeric in subtraction (-)
    > at C:\Perl\timetest.pl line ClearCase\Us
    Stuart Clemons Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139