Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how long does for(1..$n) take?

by jptxs (Curate)
on Feb 16, 2001 at 04:19 UTC ( [id://58746]=perlquestion: print w/replies, xml ) Need Help??

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

I want to instruct a script to do something for about one minute. I was using for(1..100000000), but now I want to reign it in a bit and do this for a more specific but not exact amount of time.

I've looked around and can't really find anything (non-module), to do that with, and find myself wondering if there's any way to figure out about how long the for takes. I really can't brute force time it as I need to do this within the next two hours or not at all =) love those deadlines.

"A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

Replies are listed 'Best First'.
Re: how long does for(1..$n) take?
by chipmunk (Parson) on Feb 16, 2001 at 04:29 UTC
    Have you considered setting an alarm for one minute? That will be significantly more reliable than trying to precisely time a for loop, especially since the execution time of the for loop will depend on things outside the control of the program, such as how busy the CPU is with other processes.
      I got so hung up on the for thing I didn't even think of alarm. perfect, thanks.
      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
Re: how long does for(1..$n) take?
by AgentM (Curate) on Feb 16, 2001 at 04:31 UTC
    Whoa. Careful there. Not only is this wasteful and platform-dependent, but it is also load dependent. If your computter is doing something else in another process, this loop might as well take three hours instead of the intended minute. In ComputerLand, it's good to be precise, so I would recommend alarm(60); for an interruptable process. Don't even bother setting up a signal handler if unneeded. After 60 seconds, the script will crash and burn as intended.

    If your thing cannot be interrupted, then keep track of the Time.

    while($runningtime<$maxtime) { dosomething(); }
    which is still polling (which is generally bad), but at least after you finish that iteration, you'll know whether to quit or not.

    Another another option may be to use perlembed to embed an interpreter in a pthread, which is sent an internal signal to finish from another thread after 60 seconds of sleep. The murdered thread can have cleanup handlers installed to ensure that the important stuff got done and now you can finish. I would consider this the most professional way. Good luck!

    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: how long does for(1..$n) take?
by MrNobo1024 (Hermit) on Feb 16, 2001 at 04:30 UTC
    If you want to make code loop for 60 seconds, you don't need for, you can just use time:
    my $start = time; while(time < $start + 60) { # code goes here }
Re: how long does for(1..$n) take?
by arturo (Vicar) on Feb 16, 2001 at 04:30 UTC

    Now, if only there were a builtin function that would cause the script to pause for a specified number of seconds ... like, say, this one?

    =)

    As far as the general question goes, timing with a for loop seems subject to system load as well as the more obvious things (like available RAM, processor type and speed, etc.). Even if you benchmarked it, how could you ever rely on the system being in the same state as it was when you benchmarked it?

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      sleep won't do it. I want it to actually *do* something for that period of time. Actually, i want it to spike CPU for that period of time, so sleep is quite unsuited to that =)
      "A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche
        alarm(), as suggested earlier, will work just fine. It sounds as though you are building some sort of test suite, though.

        In that case, to keep it modular, it may make sense to fork a child and have the parent sleep(60) then kill the child.

        Update: This also has the advantage of returning all the memory back to the OS after your malloc() fest. TIMTOWTDI

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://58746]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found