Initially I wrote an answer related to PHP part, suggesting the usage of dedicated functions for reading and writing csv, fgetcsv()
and fputcsv()
respectively, but I didn't test the code. Thanks to @akki who pointed out to some errors and the profiling results, I learned that these functions are dramatically slower, as explained in this answer. A code using then run 20It looks like fgetcsv() is 40 times slower than fread/explode. However, to parse the code fromproper csv, with field delimiters and escaping, you have to use the OPproper function anyway.
So we can conclude that the PHP partHere is already optimized and hardly can be improved performance-wisethe code
<?php
$t = microtime(1);
$i_fp = fopen("inFile.csv","r");
while(!feof($i_fp)) {
$line = fgets($i_fp);
$split = explode(';',$line);
}
echo "fgets: ".round(microtime(1)-$t,2)."\n";
$t = microtime(1);
$i_fp = fopen("inFile.csv","r");
while (($split = fgetcsv($i_fp, 512, ';')) !== FALSE)
{
}
echo "fgetcsv: ".round(microtime(1)-$t,2)."\n";
outputs for me
fgets: 2.1
fgetcsv: 84.45
on a file with 1 mil rows