Solved

Time in Milliseconds

Posted on 1999-01-27
5
11,319 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now