Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Get Time Diff *without* Date::Manip

by Anonymous Monk
on Nov 17, 2003 at 17:02 UTC ( #307729=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

G'Day Monks,

I have a script that does some complex math and parsing. I need to be able to tell how long it took to do it's work. This should be relatively simple: just get a timestamp before I call the sub that does the work, then get another after, and use Date::Manip or Date::Calc to figure out the difference between the two. However, I'm in a situation where I am strictly not allowed to use any external modules. Is there a perlish way to accomplish this without using any modules? I thought perhaps someone had run into this before and could point me to a recipe...

Replies are listed 'Best First'.
Re: Get Time Diff *without* Date::Manip
by Corion (Pope) on Nov 17, 2003 at 17:08 UTC

    I don't understand why you have to use Date::Manip or Date::Calc to calculate the difference between two timestamps :

    use strict; my $start = time(); do_stuff_that_takes_long_time(); my $end = time(); my $difference_in_seconds = $end - $start; print "The process took $difference_in_seconds second(s)\n";

    perldoc -f time tells you what you need to know. And you should use the Perl documentation more often for your homework - it makes no business sense to disallow any module.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: Get Time Diff *without* Date::Manip
by ehdonhon (Curate) on Nov 17, 2003 at 17:36 UTC

    I guess it depends on what you mean by "External". The Benchmark module is part of the core Perl distro, and does exactly what you want. (from the pod):

    use Benchmark; $t0 = new Benchmark; # ... your code here ... $t1 = new Benchmark; $td = timediff($t1, $t0); print "the code took:",timestr($td),"\n";
Re: Get Time Diff *without* Date::Manip
by Anonymous Monk on Nov 17, 2003 at 17:19 UTC
    my $start = (times)[0]; long_running_sub(); my $delta = (times)[0] - $start; print "Elapsed time: $delta seconds\n";
Re: Get Time Diff *without* Date::Manip
by meetraz (Hermit) on Nov 17, 2003 at 18:10 UTC
    You didn't mention whether you were on Win32 or Linux. The time() function will let you calculate elapsed time in 1-second increments.

    On Win32, if you want something with more resolution than that, you can use the Win32::GetTickCount() function, which is built-in and doesn't require you to use any modules.

    my $start = Win32::GetTickCount(); long_function(); my $end = Win32::GetTickCount(); printf("function took %01.3f seconds\n", ($end - $start) /1000);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://307729]
Approved by Grygonos
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2017-01-23 16:08 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (194 votes). Check out past polls.