my $start_time = [Time::HiRes::gettimeofday()];
my $diff = Time::HiRes::tv_interval($start_time);
print "\n\n$diff\n";
相关问题
- $ENV{$variable} in perl
- How to account for clock offsets in a distributed
- Is it possible to pass command-line arguments to @
- Redirecting STDOUT and STDERR to a file, except fo
- Java Instant.parse on Date java 8
相关文章
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Extracting columns from text file using Perl one-l
- Lazy (ungreedy) matching multiple groups using reg
- How do I tell DBD::mysql where mysql.sock is?
- What is a good way to deploy a Perl application?
- Get POSIX epoch as system_clock::time_point
This is useful for in-service timings, granularity of one second:
At start...
...anywhere and everywhere you like...
...end of program...
...and with your subs:
Depends on what you are doing. If you want to measure wall clock time (the amount of actual time that has elapsed) you can't get much better. If you want to measure how long the computer has been doing something then you might want to look at the
times
function or thetime
command. Thetimes
function in Perl returns a list of the current accumulated time for this process in your code and the code of any modules you are using, this process in system calls, all of this process's children in user code, and all of this process's children in system calls.The
time
command in UNIX is similar in function. You run a command like thisand it outputs something like this
where real is the wall clock time and user and sys are the same as user and system above.
The
time
command is easier for a human to use, but thetimes
function gives you more information and is easier to fit into a computer program (plus it has the benefit of producing results while a program is still running).Oh, I forgot to mention
$^T
. This variable holds the start time of the program in seconds since the epoch, so if you only care about a granularity of seconds you can just saynear the top of your program.
That's pretty much it -- that will give you high resolution elapsed time. Unless, of course, someone messes with the system clock.
Possibly. Depends on what you mean by "better".
If you are asking for a "better" solution in terms of functionality, then this is pretty much it.
If you are asking for a "better" in the sense of "less awkward" notation, then know that, in scalar context,
Time::HiRes::gettimeofday()
will return floating seconds since epoch (with the fractional part representing microseconds), just likeTime::HiRes::time()
(which is a good drop-in replacement for the standardtime()
function.)or: