fgetcsv() is more slow than file_get_content()/explode()

Hi guys, and girls for sure;)

I tried to parse huge csv file a little before and found interesting thing: fgetcsv is considerable slower than file processing – just take a look at my code, it was faster for me than just yummy fgetcsv():

$content = file_get_contents('somefile.csv');
$rows = explode("n", $content);
foreach ($rows as $row) {
	$columns = explode(';', $row);
 
	// Making something with $columns
 
}

… and that’s it.

Books to read

2 Comments

  1. jl

    Thanks man, I haven’t run any timing tests between the two issues but I’m switching to your method.

    Either way I don’t like keeping fopen() handles for a long time, so I prefer just loading the file into RAM and processing it in the background.

    One problem with your method is that any quoted-field may contain semi-colons, which will fail with explode()…

Leave a Reply

Your email address will not be published. Required fields are marked *