Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to time your statements in perl ?????

Status
Not open for further replies.

vjcyrano

Programmer
Dec 27, 2005
37
US
I would like to time few perl statements and then re-run them if they exceed a fixed amount of time.

eg.


{

foreach $var(@somearray)
{
print $var;
}

}



What i want to do is time this block and if this block exceeds more than say 10 minutes to run, I want to come out of the block and do something else. ( For this block you can keep a statement inside to check, but generically I want to do it for any block not just this one).

Is there a way I can do this in perl without using threads or forks ?


Thanks.....
 
I beleive you can use break;

so you could set a var to = time + seconds to run; before starting the loop

Code:
$time = time + 600; # 600 seconds = 10 mins
 
for(@array){

print $_;

 if(time > $time){
   break;
 }

}


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
A more generic way to do this is to use the alarm function:


This enables you to break out of any locked up code after set number of seconds. It does not require the intermittent check of the time which assumes that it is only the cumulative nature of your code that is taking up time, instead of individual commands.
 
there is no native "break" function in perl that I know of. You would use "last" instead of "break" for a loop.

- Kevin, perl coder unexceptional!
 
there is no native "break" function
which is odd because I googled and a whole bunch of results says there is?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Hi,

Kevin is correct, it's last and not break.

If you've written working code using break then... that surprises me. Do you have a function of your own named break?

Mike

The options are: fast, cheap and right - pick any two... [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
no , I did some googling and it seems break was changed to last, i must have been reading an old example of breaking a loop.

anyways, the logic works even if i got a keyword wrong, what's a little syntax error between friends eh ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Quite so :)

And my apologies, the answer to your original question is the standard module Time:HiRes, the examples in the documentation seem to cover what you need.
Code:
use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);

$microseconds = 750_000;
usleep($microseconds);

# signal alarm in 2.5s & every .1s thereafter
ualarm(2_500_000, 100_000);

# get seconds and microseconds since the epoch
($s, $usec) = gettimeofday();

# measure elapsed time 
# (could also do by subtracting 2 gettimeofday return values)
$t0 = [gettimeofday];
# do bunch of stuff here
$t1 = [gettimeofday];
# do more stuff here
$t0_t1 = tv_interval $t0, $t1;

$elapsed = tv_interval ($t0, [gettimeofday]);
$elapsed = tv_interval ($t0); # equivalent code

Mike

The options are: fast, cheap and right - pick any two... [orientalbow] & [anakin]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top