I am doing a lot of file searching based on a criteria in multiple iterations from my Perl script and it seems to take up 100% of the CPU time. Is there a way to control my script CPU utilization? I read somewhere about putting empty sleep cycles in my script. But I am not sure how to do this.
you could lower the process (Perl's) priority, as assigned by the OS: windows or linux
example for lowest priority:
windows
start /LOW perl <myscript>
linux
nice +19 perl <myscript>
A great way to improve CPU utilization is to use better algorithms. Don't guess where your code is spending all its time: use a profiler. Devel::NYTProf
is a fantastic tool for this.
Be sure to keep Amdahl's law in mind. For example, say part of your program uses a quadratic algorithm and with some effort you could replace it with a linear one. Hooray! But if the code in question accounts for only 5% of the total runtime, your most heroic effort can bring no better than a tiny five-percent improvement. Use a profiler to determine whether opportunities for greater speedup are available elsewhere.
You don't tell us what you're searching for, and even the best known algorithms can be CPU-intensive. Consider that your operating system's scheduler has been written, hand-tuned, tested, and rewritten to use system resources efficiently. Yes, some tasks require specialized schedulers, but such cases are rare—even less likely given that you're using Perl.
Don't take it as a bad sign that your code is eating up CPU. You may be surprised to learn that one of the hardest challenges in realtime systems, where performance is crucial, is to keep the CPU busy rather than idling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With