1

I was just wondering which is considered to be more efficient in PHP.

I need to loop through MySQL results and output it to a file using a specific format, I am interested to know whether it's more efficient to create a huge string (I'm going to be going through over 100,000 rows) and write the string to a file, or write (append) what I want to add to the file using fwrite, as I'm going through the results?

From the outset, I believe using a very long string will be more efficient than keep writing to a file but I am wondering whether PHP is going to have any issues with processing a very long string?

Thanks.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
  • So, basically your question is not what you have mentioned but whether PHP has any problem in handling (very)long strings or not. Right? – Bhavik Shah Jan 25 '13 at 11:52
  • @BhavikShah No, my question is what I stated. Which is more efficient? I believe that building up a huge string will be more efficient, but I'm not entirely sure and my worry concerning it is whether PHP has any string limitations. – EM-Creations Jan 25 '13 at 11:54

2 Answers2

3

PHP wont have an issue with creating a very long string, it will however use a lot of memory. As long as you have enough memory available then this won't be an issue. Reading and writing from memory is much faster than writing to disk so going down the memory route could be an option although with 100,000 rows, you could split it up and get the best of both worlds.

What I mean by this is every 10,000 rows append your string into the file and then empty the string, this way, you'll only be writing to file 10 times and storing a smaller amount of data in memory.

Prisoner
  • 27,391
  • 11
  • 73
  • 102
1

If you create a large string, try to collect all values into an array and join it together before writing. Strings are immutable, if you just append values to a String, this will lead to bad performance.

Best solution would be to collect for example 1000 results, write this part to a file, collet another 1000 results and so on.

Gregor
  • 4,306
  • 1
  • 22
  • 37