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

while running the Perl script in my windows system, the CPU usage is 100 percent and the system is getting slow down. I would like to know is there any code available to reduce the CPU usage and make the system faster.

Replies are listed 'Best First'.
Re: cpu usage
by cdarke (Prior) on May 17, 2010 at 08:19 UTC
    100% CPU usage could mean that you are using the system efficiently. However lets assume that is not the case.

    You need to look at what your script is doing, there is no magic performance improvement pill. Of course I have no idea what that might be, or how complex the script is. A start would be to profile it using Devel::DProf to find the bottlenecks, however a small script might just need an inspection.

    It could be that CPU is not really an issue, but that you are paging too much, so look at the size of your data structures - particularly if you are reading an entire file into memory. Another common error is creating too many child processes, especially within a loop. Runaway CPU usage might mean an infinite loop, so check your loop exit conditions - and possibly step through using the debugger. Inserting sleep statements might seem attractive but they can mask other errors.

    If you really have no alternative and your script is error free (!) then you can reduce its priority using Windows Task Manager or by calling the Win32 API SetPriorityClass.
Re: cpu usage
by Xilman (Hermit) on May 17, 2010 at 08:15 UTC

    There are two separate issues here. The first is that the script is taking 100% of the cpu. The other is that the system is slowing down. Without knowing more details about your script it is difficult to give concrete advice.

    If the slowdown arises from scheduling your script in addition to the other processes you wish to run, you could try running it at lower scheduling priority. Either use "START /LOW ..." or reset the scheduling priority with the task manager. Type "START /?" in a command window for more assistance.

    If the slowdown arises from paging and/or swapping, you either need to add more RAM to your system or rewrite the script to use less memory. Without knowing more details about your script ...

    Paul

Re: cpu usage
by choroba (Cardinal) on May 17, 2010 at 07:48 UTC
    What script are you running? There might be ways to optimize the script.