Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all: I'm trying to determine how long an system operation takes. Anyone know of a simple way to do this ? I wanted to establish the start time. Then run the operation. Then mark the finish time. Then substract the start time from the finish time to get an elapsed time. Here's the simplistic approach I tried. I'm sure I need a time that is measured in seconds or something like that, but I'm not sure how to do this. Here's what I tried:
#!/usr/bin/perl -w use warnings; use strict; my $start = "Tue Jan 27 15:40:16 2004"; print "This is the start time: $start \n"; system (This is where the system process stuff goes); my $finish = localtime; print "This is the finish time: $finish \n"; my $elapsedtime = ("$finish" - "$start") ; print "This is the time diff: $elapsedtime \n"; The above obviously didn't work. Here's what it returned: This is the start time: Tue Jan 27 15:40:16 2004 Argument "Tue Jan 27 15:40:16 2004" isn't numeric in subtraction (-) a +t C:\Perl\timetest.pl line 13. This is the finish time: Tue Jan 27 19:45:56 2004 This is the time diff: 0 Argument "Tue Jan 27 19:45:56 2004" isn't numeric in subtraction (-) a +t C:\Perl\timetest.pl line ClearCase\Us
PS. I can't use modules so I will have to do it that way , can someone advice please thanks

Replies are listed 'Best First'.
Re: elapsed times
by Roy Johnson (Monsignor) on Feb 05, 2004 at 22:49 UTC
    Use time at the start and end, and take the difference. It is in seconds.

    The PerlMonk tr/// Advocate
Re: elapsed times
by Corion (Patriarch) on Feb 05, 2004 at 22:42 UTC

    Perl has the Benchmark module, which is even part of the standard distribution of Perl. So most likely you will want to use that module.

Re: elapsed times
by parkprimus (Sexton) on Feb 05, 2004 at 22:49 UTC
    I don't know if this will apply but if you have a linux/unix system you could type "time perl name_of_your_script.pl" at the command prompt. This will give you the real, user, and sys time in milliseconds. If this is not on linux and you can't install modules then your first problem is that you are trying to substract Tue Jan 27 15:40:16 2004 from Tue Jan 27 19:45:56 2004. This will give you an error, hence the zero. So you need to parse out the time and subtract just the numbers.