0

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?

Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
  • 1
    possible duplicate of [PHP code's performance test](http://stackoverflow.com/questions/2919543/php-codes-performance-test) – Jay Blanchard Jul 24 '14 at 17:17
  • 1
    `fwrite($handle, implode($arr));` - no overhead of assigning to a string first – Mark Baker Jul 24 '14 at 17:17
  • @JayBlanchard Thanks for the link. While that would be an alternative way to figure this out, I don't think it qualifies as a duplicate. I'm also asking because I'm hoping to hear some reasoning for why a certain way is faster, rather than just evaluate based on raw speed data. – Chris Middleton Jul 24 '14 at 17:18
  • I really wonder if this speck of a performance is critical to you. – Prasanth Jul 24 '14 at 17:19
  • Reasoning for why certain things are faster are speculative and opinion based due to the fact that there may be system differences, version differences, etc. @AmadeusDrZaius Have you done in perf testing on these? If so, you can likely draw your own conclusions as to speed (as a generalization). – Jay Blanchard Jul 24 '14 at 17:23
  • @JayBlanchard I could try but it would probably take me days as I have no experience with doing that in PHP and from the question you linked to, the available options are meager. By posting here, I was hoping to get a more knowledgeable opinion from someone who has some actual experience with PHP internals... or at least someone who is more experienced than me. Isn't that what SO is for, anyway? Learning from each other? As I get better, I will answer more questions from people that *I* know more than, and the cycle repeats. I don't see anything wrong with that. – Chris Middleton Jul 24 '14 at 17:25
  • SO is specifically not for opinion based answers @AmadeusDrZaius http://stackoverflow.com/tour IMHO learning how to perf test is part of what one should do if one wants to become a better programmer. – Jay Blanchard Jul 24 '14 at 17:28
  • I've read that before. This is not an opinion-based answer ... in my opinion. :P It's an answer that may depend on implementation, but maybe it doesn't. In either case, that's not an issue of *opinion*. I didn't say "Which method is most fun to use?" or something like that. Efficiency is measurable and/or calculable. EDIT: I see that I used the word "opinion" in my previous comment, and that's why you're jumping on that. That was the wrong word to use. It should say "more knowledgeable perspective" or info. – Chris Middleton Jul 24 '14 at 17:29

1 Answers1

3

Not sure about fastest/most efficient; but this opens the file, writes imploded data to the file and closes the file in one function:

file_put_contents('/path/to/file.txt', $arr);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • 1
    I thought this had an error, but then I read this on php.net: _"You can also specify the data parameter as a single dimension array. This is equivalent to file_put_contents($filename, implode('', $array)). "_ – TecBrat Jul 24 '14 at 17:29
  • It says in the manual entry that this may cause concurrency problems. Does that mean that as long as I don't try to read the file after the write in the same program execution, I'll be okay? – Chris Middleton Jul 24 '14 at 17:31
  • I would think in that case using `LOCK_EX` as third arg may solve the issue. – AbraCadaver Jul 24 '14 at 17:33
  • I see, thanks. For anyone else reading this, it's unclear to me whether LOCK_EX also prevents reading, but [`flock`](http://php.net/manual/en/function.flock.php) provides full file locking capabilities. – Chris Middleton Jul 24 '14 at 17:51