Solved

How do I calculate Time elapsed between two processes in perl script in Milliseconds?

Posted on 2008-10-15
11
1,971 Views
Last Modified: 2013-12-26
I have written perl program in which I want to check time elapsed between beginning and ending of STDIN input.For ex-If I input password from user , I need to calculate how many milliseconds user took to input password
I can realize this in seconds and microseconds using time() , date() and perl module Time::HiRes
Is there any way to calculate in Milliseconds?
0
Comment
Question by:saini_er
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22723147
Note that 1 millisecond is 1000 microseconds.  So if you have the elapsed time in microseconds, multiple that by 1000 to get the elapsed time in milliseconds.

Or you can get the time in seconds (using time() from Time::HiRes ), and divide that by 1000.
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 22723148

use Time::HiRes;
my $start = [ Time::HiRes::gettimeofday( ) ];
 
## Your code goes here
 
my $elapsed = Time::HiRes::tv_interval( $start );
print "Elapsed time: $elapsed seconds!\n";

Open in new window

0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 22723158
Or use Benchmark - this is a great way to do all sorts of timer tests.
http://search.cpan.org/~andrewho/Benchmark-Timer-0.5/Timer.pm
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 6

Expert Comment

by:RSLE
ID: 22723288
1 millisecond = 1000 microseconds, thus divide your HiRes microsecond time() by 1000.
0
 

Author Comment

by:saini_er
ID: 22723418
Thanks everyone for quick response but I am working on time sensitive application where I need to see time difference at millisecond level and do comparison with previous results with room of few milliseconds variation. Definitely , divide/multiply will not give accurate result in milliseconds.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22723530
If you are using the Time::HiRes module, and get the time in microseconds, multiplying this my 1000 will be very accurate.  Why would you think otherwise?
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 22724018
Please note that the time value that Time::HiRes() gives you is a floating-point integer, so you don't lose any decimals due to rounding. Eg: 1.54666574 * 1000 = 1546.66574 milliseconds.

It's not like you're getting a time integer in seconds (1.0).
0
 

Author Comment

by:saini_er
ID: 22724778
Is there any way to get result in real number i.e. 156 ms instead of 156.66574 ms  if I multiply/Divide by 1000
By using Microseconds , I am getting very inconsistent results. i.e. like 30000 microsec and then 435432 microseconds.So I am not sure If I round up this thing , will it be still accurate enough
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22724935
>>real number
I think you mean integer - a whole number

Normally, perl takes care of handling a number as an integer or a float automatically.  If you want to force it to do this as an integer, you can "use integer", and then call gettimeofday, which will return (in list context) the seconds and microseconds as an integer.

This isn't necessary though... here is a code snippet that shows demonstrates how to time how long it takes to get user input, in milliseconds (displayed as both a float and an integer)


use Time::HiRes qw[gettimeofday tv_interval];
 
my $time1=[gettimeofday()];
print "Enter something: ";
my $user=<STDIN>;
chomp $user;
my $milliseconds = tv_interval($time1)*1000;
 
 
print "User entered '$user'\n";
print "User tool $milliseconds milliseconds\n";
printf "User took %d milliseconds\n", $milliseconds;

Open in new window

0
 

Author Closing Comment

by:saini_er
ID: 31506382
Thanks a lot.It seems to be working for me
0
 

Author Comment

by:saini_er
ID: 22727822
Thanks everyone for your time.I really appreciate...I think conversion will work for me...I wish if there was direct function to tell milliseconds which could have given more accurate answer..
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 157
improve Shell script 5 149
how to split multiple lines delimiter : 8 97
parse a file and get data out 11 40
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

813 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

18 Experts available now in Live!

Get 1:1 Help Now