XML xpath search and array loop with php, memory issue

I am dealing with large XML files (several megabytes), for which I have to do various checks. However, I have a problem with memory and time, which is growing very fast. I tested it like this:

$xml = new SimpleXMLElement($string);
$sum_of_elements = (double)0.0;

foreach ( $xml->xpath('//Amt') as $amt ) {
  $sum_of_elements += (double)$amt;
}

With the microtime () and memory_get_usage () functions, I get the following results by running this code:

  • 5Mb file (7480 elements):
    • run time 0.69 s
    • Memory usage increases from 10.25Mb to 29.75Mb

It's all right. But then there is a lot of time with a bit more file memory and time use:

  • 6Mb file (8976 elements):
    • lead time 8.53s
    • Memory usage increases from 10.25Mb to 99.25Mb

, . for-loop foreach, . .

, ?

+1
1

SimpleXML . unset PHP GC . , XMLReader pull-based. XPath, .

+1

Source: https://habr.com/ru/post/1615078/


All Articles