Solved

Time in Milliseconds

Posted on 1999-01-27
5
11,325 Views
Last Modified: 2008-03-17
I'm using a perl script to time things with code like $startmain=time; at the beginning and at the end of what I want to time.  Time seems to be limited to a minimum value of seconds, while I would like to measure down to the millisecond.  Is there a way to do this?

One oddment I found also is that when I want to establish the date and I use gmtime(time)=($var ...), I have to increment the month by 1 to get the current month.  Does anyone know why this is?
0
Comment
Question by:wilfrieds
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1210327
use perl's times builtin command (see Camel book).

> I have to increment the month by 1 to get the current month
'cause the month is returned in C-style indexing: 0..11
0
 
LVL 1

Author Comment

by:wilfrieds
ID: 1210328
ahoffmann : times seems to provide me only with CPU times, where for this particular reason I want the total lapse time of the script - is there something else you are aware of?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1210329
perldoc -q 'time.*second'

=head1 Found in perlfaq8.pod

=head2 How can I measure time under a second?

In general, you may not be able to.  The Time::HiRes module (available
from CPAN) provides this functionality for some systems.

In general, you may not be able to.  But if your system supports both the
syscall() function in Perl as well as a system call like gettimeofday(2),
then you may be able to do something like this:

    require 'sys/syscall.ph';

    $TIMEVAL_T = "LL";

    $done = $start = pack($TIMEVAL_T, ());

    syscall( &SYS_gettimeofday, $start, 0)) != -1
               or die "gettimeofday: $!";

       ##########################
       # DO YOUR OPERATION HERE #
       ##########################

    syscall( &SYS_gettimeofday, $done, 0) != -1
           or die "gettimeofday: $!";

    @start = unpack($TIMEVAL_T, $start);
    @done  = unpack($TIMEVAL_T, $done);

    # fix microseconds
    for ($done[1], $start[1]) { $_ /= 1_000_000 }

    $delta_time = sprintf "%.4f", ($done[0]  + $done[1]  )
                                            -
                                 ($start[0] + $start[1] );

0
 
LVL 1

Author Comment

by:wilfrieds
ID: 1210330
thanks ozo
i 'll accept your answer if you care to post your comment as an answer
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 1210331
If you're trying to benchmark a piece of code, you might also
use Benchmark;
which lets you run the code a number of times to get a more precise timing.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extract multiple value with delimiters from a string 4 170
pattern matching in perl 2 103
collecting information 2 158
PHP equivalent of opening a com object 5 83
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question