Home  >  Article  >  Backend Development  >  Solution to the problem that PHP array consumes too much memory_PHP tutorial

Solution to the problem that PHP array consumes too much memory_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:39:02974browse

Because this is a language problem, conventional solutions are more difficult to solve. The following is a solution through string.

Copy code The code is as follows:

$total = 100000;
$double = "";
for ($i = 0; $i < $total; $i++)
{
$double .= pack("d", $i + 0.1);
}
for ($i = 0; $i < $total; $i++)
{
unpack("@" . ($i * 8) . "/d", $double);
}

This example uses a string to save an array of doubles. Then unpack it when using it.
Of course, this will impact performance. It depends on the specific needs.

For example, in this case:
You have 10 arrays, each array is 10M (about 1 million data), then ten will cost 100M of memory.
If you add 10 people concurrently, the memory will be seriously insufficient.
Then, among the 10 arrays, they are not used at the same time every time. You can save them as strings
and then, when using, unpack a certain string into an array.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321625.htmlTechArticleBecause this is a language problem, conventional solutions are more difficult to solve. The following is a solution through string. Copy the code The code is as follows: $total = 100000; $double = ""; for ($i =...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn