?
Solved

Time in Milliseconds

Posted on 1999-01-27
5
Medium Priority
?
11,334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans
Suggested Courses

752 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