Say I have an array $arr
and I want to write its contents to a file $handle
. What's the fastest/most efficient way to do this in PHP(5)?
Some different options:
Convert the array to a string using implode:
$string = implode($arr);
fwrite($handle, $string);
Write it to the file char by char (seems like it would be slower to me):
foreach($arr as $char) {
fwrite($handle, $char);
}
Concatenate using the .
operator and then write:
$string = '';
foreach($arr as $char) {
$string .= $char;
}
fwrite($handle, $string);
The third seems like it would be slowest to me, as I'm guessing that fwrite and implode are written in C... but then again with JIT-compiling these days maybe it's optimizing the concatenations with no function call overhead.
Which of these -- or some other way -- is the fastest and why?