N
N
Nikelamoc2015-09-26 09:36:00
PHP
Nikelamoc, 2015-09-26 09:36:00

What is the least resource intensive way to write an array to a file?

Good afternoon.
It is necessary to write the array to a file with minimal RAM and CPU costs.
Now it's done like this

foreach($array as $key => $value) {
$price_array[$key] = $value[price];
}
$filename = dirname(__FILE__) . "/data/price.txt";
file_put_contents($file, '<?php $price_array = ' . var_export($price_array, true) . ';');

I understand that you need to do something through fwrite, but how to pack the line there?
I tried through serialization, but a broken array comes out.
And most importantly, if there is an array of 50k values, will the script not hang up such a line-by-line writing to a file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin, 2015-09-26
@Nikelamoc

Strongly depends on the structure of the dictionary. If only string key-values, then you can try to write it like this: https://gist.github.com/sc0rp10/0cef7c64aad392a2e799 .
Then I advise you to overlay yourself with profilers and find out for yourself what is more profitable: write line by line from a large dictionary in memory, or dump it into a file once. Well, there already to draw a conclusion that specifically in your case is more suitable.
Personally, it seems to me that it is worth thinking about the execution speed of var_export + file_put_contents, for example, from a data volume of hundreds of megabytes. In the sense that on smaller volumes, these calls will take extremely little time against the background of the total execution time and there will be no point in literally saving on matches.
But, I repeat, everything should be profiled, not guessed.

D
Dmitry Korolev, 2015-09-26
@Apathetic

Well, you have already tried two methods (serialization and var_export), now try json_encode. This method works with about the same performance as serialization, and many times faster than var_export. Keep in mind a couple of limitations: json_decode will work with errors if the array has more than 127 levels and there will be a problem with restoring the object as, in fact, an object. But, as I understand it, in your case this is not a problem. But there is an additional plus - json_encode gives a smaller string at the output than serialize.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question