I don't see a speed problem with your code here is what I would do for testing to locate the speed issue.
insert this function into the file in question
<?php
//lifted directly from the manual
function getMicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
} //end getMircotime
?>
for the first line of your script put this
$log_file = <path and name of log file>
$start = getMicrotime();
for the last line of your script put this
$end = getMicrotime();
$fp = fopen("benchmark.log",'a');
$now = date("m-d-Y h:m.s");
$ip = $_SERVER['Remote_Addr'];
$runtime = sprintf('%.16f', $end - $start);
fwrite($fp,($now . " [" . $ip . "] " . $runtime . "\n");
fclose($fp);
this will let you know if your file is the problem. It will also track ip, date and time of day with the tiem it takes the script to run.
The problem might not be the script this will diagnose that much for you.
If you run this and you're getting high runtimes then your script is the problem. If this is the case start moving the $start = getMicrotime() and $end = getMicrotime() lines of code closer and closer together until you find the code that is causing your problem.