Answer the question
In order to leave comments, you need to log in
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) . ';');
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question