Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: timer without modules

by Random_Walk (Parson)
on Nov 09, 2009 at 15:27 UTC ( #805945=note: print w/ replies, xml ) Need Help??


in reply to Re^4: timer without modules
in thread timer without modules

time() returns the number of seconds since the epoch. When your function is called check if the value from time() is greater than the previous stored value + 60 seconds. If there is no previous value 0|undef + 60 will certainly be less than time() returns.

your problem may be more with deciding which data structure you want to use to store these time stamps for each user. A global hash keyed on user name would be the simplest if a little cludgy.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!


Comment on Re^5: timer without modules
Re^6: timer without modules
by elunatic (Initiate) on Nov 09, 2009 at 21:37 UTC

    you know, that is pretty much exactly what i was thinking, and i tested a method like that locally on my pc.. *with nice results*

    the only problem is with how i did it in testing, which was a continuous while loop, this increased my cpu usage from 3-5% to 60%.
    but i figure this has to do with the loop right? also im on windows testing before i move to the shell. *dont wanna make the provider angry lol*

      Just do the test and store the time when the user calls the function.

      # code not tested use strict; use warnings; my %called; my $interval = 60; # how long before they can call it again sub something { my $user = shift; # pass the function the name of the user calling + it my $now = time; return if exists $called{$user} and $now < $called{$user} + $inter +val; $called{user} = $now; # do the costly funtion }

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://805945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2014-11-28 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (194 votes), past polls