I'm not overly efficient when it comes to coding, and I am looking for ways to improve the speed of a log translator I built.
Several files are uploaded to a folder on a server and log files are selected for processing. Speed here is fine. Each log file has 5000-10000 lines possible a bit higher. They are space delimited files that look like this:
20:04:13 + MA 00 61
20:04:17 - MA 00 61
20:04:18 + MA 00 61 optionaltxt
20:04:20 - MA 00 61
There are at least 5 fields for every entry but more can be added if optional text exists. Field 5 is a status code, and it has to be cross referenced with a language file then replaced with the corresponding message so the result is like this:
17:29:48 - MA 00 (061) Feed hold! X / Y axis is locked
17:29:48 + MA 00 (061) Feed hold! X / Y axis is locked
17:29:50 - MA 00 (061) Feed hold! X / Y axis is locked optionaltxt
17:29:51 + MA 00 (061) Feed hold! X / Y axis is locked
The translated logs are saved to a new folder for download later. At the moment translation takes about 7-10 seconds per log file. The problem is that it is common to have 90 files or more and the time starts adding up. I have no control over the log format or language file format.
This part of my code looks like this:
$shLANG = file_get_contents("ErrCODES/data_en.properties");
$SHarray = explode("\n", $shLANG);
$logARRAY = glob($uploadDIR.$filepath."/original/*.[lL][oO][gG]");
foreach ($logARRAY as $logFILEarray) {
$name = basename($logFILEarray);
$logFILE = file_get_contents($uploadDIR.$filepath."/original/".$name);
$logFILEarray = explode("\n", $logFILE);
foreach ($logFILEarray as $key => $line) {
$newline="";
$LINEarray = explode(" ", $line);
if(isset($LINEarray[0])){$newline .= $LINEarray[0]." ";}
if(isset($LINEarray[1])){$newline .= $LINEarray[1]." ";}
if(isset($LINEarray[2])){$newline .= $LINEarray[2]." ";}
if(isset($LINEarray[3])){$newline .= $LINEarray[3]." ";}
if(isset($LINEarray[4])){
foreach ($SHarray as $code) {
if (strlen($LINEarray[4])>0){
if (substr($code, 0, strpos($code, '=')) == $LINEarray[4]) {
$newline .= trim(explode('null;', $code)[1]). " ";
}
}
}
}
if(isset($LINEarray[5])){$newline .= $LINEarray[5]." ";}
if(isset($LINEarray[6])){$newline .= $LINEarray[6]." ";}
if(isset($LINEarray[7])){$newline .= $LINEarray[7]." ";}
if(isset($LINEarray[8])){$newline .= $LINEarray[8]." ";}
$newline .= "\r";
$logFILEarray[$key] = $newline;
}
//var_dump($logFILEarray);
$info = implode("\n",$logFILEarray);
file_put_contents($uploadDIR.$filepath."/translation/".$name, $info);
}//for all glob .log
The code works fine, but how can I improve the speed (greatly)?
file_get_contentsandexplode, have you considered reading one line at a time (this may actually be slower, though, so just asking, not suggesting). Finally, does it have to be PHP? Unix command line tools may work faster. \$\endgroup\$