seeing your code, i assume you're on a unix like machine. Try to set the nice level of your program. If it's the only running process, it still takes a lot of cpu-time, but if another program with a higher nice level gets in, it reduces it's load.
Since you use system commands, you could run them like
...
if ($nicelevel ne "") {
$cmd = "nice -$nicelevel " . $cmd;
}
system $cmd;