K
K
Khurshed Abdujalil2017-04-20 12:09:10
Yii
Khurshed Abdujalil, 2017-04-20 12:09:10

How to reduce CPU load when parsing xml?

I download files weighing from 30 to 700mb, there is often a large load on the processor, how can I solve this problem?
I use extension from Hobnob\XmlStreamReader\Parser for yii2

$parser = new \Hobnob\XmlStreamReader\Parser();
        $parser->registerCallback(
            '/source/products/product',
            function( \Hobnob\XmlStreamReader\Parser $parser, \SimpleXMLElement $node ) {
               parseListing($node);


            }
        );

further in the parseListing function I will convert to an array
function parseListing($product)
 {
        if (empty($product))
            return;

        $data = xml2array($product);

/*тут уже сохраняю к себе в базу данные из $data*/
}
function xml2array($xml)
    {
        $arr = array();
        foreach ($xml->children() as $r)
        {
            /** @var \SimpleXMLElement $r */
            if (count($r->children()) == 0)
                $arr[$r->getName()] = strval($r);
            else
                $arr[$r->getName()][] = xml2array($r);
        }
        return $arr;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
ThunderCat, 2017-04-20
@ThunderCat

most likely it will not be possible to seriously reduce the load, the xml is very redundant and heavy, and given the weight of the files that you brought, this should really load the most. Alternatively, if this is not a priority task, running it in the console with a low priority will be slower, but the processor will not be so loaded at the peak.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question